Skip to content

Conversation

@Kosta-Github
Copy link

@Kosta-Github Kosta-Github commented Jun 26, 2019

about additionalProperties usage in swagger see for example this post

  • additionalProperties can be defined for any object type
  • they are adding unnamed and potentially typed properties to a type
  • the pitfall is that not defining an additionalProperties field on an object type needs to be treated as defining it as true (see URL given above)
  • this means that many object types will add a [key: string]: any; definition which is required according to the swagger spec
  • there is no dedicated dictionary object or class => dictionary has been removed

Note, that this is a breaking change, since the type template need to adjust to the newly introduced properties hasAdditionalProperties and additionalPropertiesType!

getInventory(parameters: {} & CommonRequestOptions): Promise < ResponseWithBody < 200, {
[key: string]: number
} >> {
getInventory(parameters: {} & CommonRequestOptions): Promise < ResponseWithBody < 200, object >> {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be fixed/enhanced in a separate PR dealing with pre-defined response definitions...

export type test_add_props_12 = {

[key: string]: some_def;
};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the types test_add_props_* have been validated according to the description about how to handle the different cases for additionalProperties ...

"test_add_props_11": { "type": "object", "additionalProperties": { "type": "object", "properties": { "nested_prop": { "type": "string" } } } },
"test_add_props_12": { "type": "object", "additionalProperties": { "$ref": "#/definitions/some_def" } }
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are the new test cases

minItems: 0,
title: "any",
properties: {}
});
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know about this one? is there a better way to specify the any type?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe makeAnyTypeSpec(swaggerType) would work?

%><%#isObject%>{
<%#properties%> '<%name%>'<%^isRequired%>?<%/isRequired%>: <%>type%>;<%/properties%>}<%/isObject%><%!
<%#properties%> '<%name%>'<%^isRequired%>?<%/isRequired%>: <%>type%>;<%/properties%><%#hasAdditionalProperties%>
[key: string]: <%#additionalPropertiesType%><%>type%><%/additionalPropertiesType%>;<%/hasAdditionalProperties%>}<%/isObject%><%!
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the newly introduced hasAdditionalProperties and additionalPropertiesType fields in the template

about `additionalProperties` usage in `swagger` see for example [this post](https://support.reprezen.com/support/solutions/articles/6000162892-support-for-additionalproperties-in-swagger-2-0-schemas)

* `additionalProperties` can be defined for any `object` type
* they are adding unnamed and potentially typed properties to a type
* the pitfall is that not defining an `additionalProperties` field on an `object` type needs to be treated as defining it as `true` (see URL given above)
* this means that many object types will add a `[key: string]: any;` definition which is required according to the swagger spec
* there is no dedicated `dictionary` object or class => `dictionary` has been removed

Note, that this is a breaking change, since the `type` template need to adjust to the newly introduced properties `hasAdditionalProperties` and `additionalPropertiesType`!
minItems: 0,
title: "any",
properties: {}
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe makeAnyTypeSpec(swaggerType) would work?

@Kosta-Github
Copy link
Author

any opinions on this PR?

@Markionium
Copy link
Collaborator

@Kosta-Github I didn't merge it yet since i was wondering if you would fix the following. If you don't have a lot of time to do this for now I'm ok to merge it :D

image

@Kosta-Github
Copy link
Author

@Kosta-Github I didn't merge it yet since i was wondering if you would fix the following. If you don't have a lot of time to do this for now I'm ok to merge it :D

image

honestly, I will not have the time right now, I just came back from vacation and need to clean out several tasks accumulated so far...

@Markionium
Copy link
Collaborator

@Kosta-Github I didn't merge it yet since i was wondering if you would fix the following. If you don't have a lot of time to do this for now I'm ok to merge it :D
image

honestly, I will not have the time right now, I just came back from vacation and need to clean out several tasks accumulated so far...

Thats fine, in that case i'll merge it. Thanks a bunch for your contributions!

@Markionium Markionium merged commit c92098e into mtennoe:master Jul 17, 2019
@Markionium
Copy link
Collaborator

Published as 1.11.0

@andy-viv
Copy link

andy-viv commented Aug 2, 2019

@Kosta-Github specifically said this was a breaking change. This should have been published as 2.0.0.

@andy-viv
Copy link

andy-viv commented Aug 2, 2019

@Kosta-Github can you provide a migration guide? Or updates to the README?

}
if (
swaggerType.additionalProperties === undefined ||
swaggerType.additionalProperties === true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If additionalProperties is undefined, wouldn't it make more sense to default to false?

@Kosta-Github
Copy link
Author

@andy-viv

@Kosta-Github
Copy link
Author

@andy-viv re-reading the comments in the link above, I might come to the conclusion that for the swagger only support case, only the additionalProperties: { type: ... } case should be handled, ignoring all other cases possible with JSON schema, just like additionalProperties === undefined | true | false ...

@andy-viv
Copy link

andy-viv commented Aug 5, 2019

note that I had mentioned it within the PR description:

@Kosta-Github yes, that criticism was actually directed toward @Markionium.

Also, I figured out why this broke for my project... We had custom templates for type.mustache which i needed to updated.

@andy-viv
Copy link

andy-viv commented Aug 5, 2019

@Kosta-Github I still think we should just treat additionalProperties === undefined the same way as additionalProperties === false... I do think supporting additionalProperties === true provides some value in terms of usage in typescript types.

Barring that, I would be okay with only supporting the case where additionalProperties is an object as you said.

@Markionium
Copy link
Collaborator

Hey @andy-viv, @Kosta-Github ,

I sincerely apologise. I definitely messed this one up, sorry for the inconvenience. I read it was a breaking change when merging the PR. But somehow forgot about it when releasing it a couple days later.

I think it might be worth investing some time in a better process for this.

Just to be clear from our side on what to do. You mitigated the problem? @mtennoe went ahead and published it as 2.0.0, so you can consume that.

We considered un-publishing the version that should have been published as 2.0.0. But that would also break others that took a direct dependency on that version.

Again sorry for the inconvenience,

Mark

@andy-viv
Copy link

Hey Mark,

Sorry for the late response. No problem. For now I have had to pin to 1.10.4 because we will need to update our custom templates to match the new requirements.

I plan on opening a PR to do what I said in this comment:

we should just treat additionalProperties === undefined the same way as additionalProperties === false

Until then, we're going to leave it pinned to 1.10.4.

Thank you @mtennoe and @Markionium for the work on this project!

mohofo7 added a commit to mohofo7/swagger-typescript-codegen that referenced this pull request Oct 30, 2021
as mentioned mtennoe#86 (comment)

treat additionalProperties === undefined the same way as additionalProperties === false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants