-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core] Add type and format properties to model of inline response #6153
[core] Add type and format properties to model of inline response #6153
Conversation
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Outdated
Show resolved
Hide resolved
5114dcc
to
8698e0e
Compare
After a few experiments with schema: definitions:
Test:
properties:
propertyA:
type: string
proeprtyB:
type: string
Well, let's consider output Since it's a core bug I subscribe @OpenAPITools/generator-core-team |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a bug necessarily, since any schema without a type defined is an object implicitly. Add a type: object
to the output when it exists in the input makes sense, but consider that someone might think this is now a bug when they don't define type: object
for input but it's now in the output.
I do think it makes sense to have this, however. OAS 3.1 will have a type: null
and I wouldn't be surprised if that became the default.
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Outdated
Show resolved
Hide resolved
Thanks for the review, Jim!
Is it possible to check that input definition contains I've read OAS 3.0.3 spec yesterday and can't recall anything like "consider schema without |
@jimschubert The reason why I ask about parsing schemas without
And maybe it's not relevant to current issue directly, but @richardwhiuk said:
|
8698e0e
to
49de85e
Compare
I don't think a schema without type means it is an object implicitly. Where do you see that specified? I think no type means the type can be anything. For example below the value of foo:
type: object
properties:
int_prop:
type: integer
any_prop:
description: a property whose value can be an integer, boolean, number, array or object.
|
@@ -2090,10 +2093,12 @@ components: | |||
properties: | |||
breed: | |||
type: string | |||
type: object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I agree type: object
was missing. It it there in the original document (modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml), it makes sense to add it.
@@ -1422,14 +1422,17 @@ components: | |||
allOf: | |||
- $ref: '#/components/schemas/Animal' | |||
- $ref: '#/components/schemas/Dog_allOf' | |||
type: object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In JSON schema, allOf means a compliant validator must validate the payload against all of the nested schemas under allOf
. Since Animal is already specified as a type: object
, it's redundant and verbose to add type: object
here.
IMO, |
In #6150, you state that type is required property, but that is not correct. The type keyword is not required. |
Yeah, my mistake. Current OAS 3.0 Specification doesn't declare For me(I need to parse OAS in my own tool) it means that I shouldn't throw exceptions when schema doesn't contain |
Just FYI, the relevant sections of the JSON schema are:
and
So if a keyword is not present, there is no constraint assertion. There is nothing to guess. Also here I think it's better to use the word keyword rather than property because "property" means something else in JSON schema. |
My case is a bit different. I'm working on a data mocker. When mock tool gets input schema without |
First, let me point out we make a distinction between "any object" versus free form object. The "any type" schema is even more generic than free-form. It's not necessarily an object. It could be an object, but also an array, the null value, an integer, number, string or boolean. Are you saying the purpose of the data mocker is to automatically generate mock data based on the OpenAPI schema? |
That's how I see it. The main advantage of OAS is auto generation of application parts. OAS perfectly describes how request and response looks like. I've spent so many time on writing server unit tests which can be autogenerated. Usually I need to write fixtures of valid and invalid requests. And that is the place where mock or faker engine fits perfectly. There are developers that already shows interest in mock server. This feature helps to get demo server as soon as possible. #3545 Here is the tool I make btw - OpenAPI Data mocker. |
I would consider the following scenarios that don't have the components:
freeFormSchema:
description: free form object (must be a dictionary).
It would be very unusual to write the schema that way, but since the schema
must match ALL of the allOf nested schema, that means the top-level schema
must be an object.
allOf:
- type: object
additionalProperties: true
anyTypeSchema:
description: any type, the payload can be anything (boolean, integer, null, number,
string, object, array). Ideally the mock server would be able to generate all of
these values.
unusualSchemaWithConstraintsAndNoType:
description: this is (imo) a very unusual way to specify a schema.
The type can be anything (boolean, integer, null, number,
string, object, array)
When the value is a number, it cannot be more than 10.
If the value is a string, the length of the string cannot be more than 20.
Any boolean value is ok.
If the value is an array, it cannot have more than 30 items.
maximum: 10
maxLength: 20
maxItems: 30
nothingSchema:
description: no JSON payload can ever match this schema
allOf:
- type: string
- type: boolean
composedSchemaAllOf:
description: The type of this schema depends on the type of the nested schemas.
The nested schemas must be of the same type, except the scenario when A is
an integer and B is a number.
allOf:
- $ref: '#/components/schemas/A'
- $ref: '#/components/schemas/B'
composedSchemaOneOf:
description: The type of this schema depends on the type of the nested schemas
oneOf:
- $ref: '#/components/schemas/A'
- $ref: '#/components/schemas/B'
composedSchema:
description: The type of this schema depends on the type of the nested schemas.
The nested schemas may have different types.
anyOf:
- $ref: '#/components/schemas/A'
- $ref: '#/components/schemas/B' |
I don't think so. As far as I remember encoding |
What I mean is suppose the payload is: "hello" A compliant validator must validate the payload against all If the payload is the value |
Ohhh, now I see. Thanks for so many test cases. I asked community for weeks with no response at all. Thanks again Sebastien! |
So, in a conclusion I need to fix behaviour with 4 keywords(
Model:
# type can be omitted because of allOf[1] schema
type: object
allOf:
- properties:
foobar:
type: string
- type: object
properties:
foobaz:
type: string
Model:
# type can be omitted because of allOf[1] schema
type: object
anyOf:
- type: string
- type: object
Model:
# type can be omitted because of allOf[1] schema
type: object
oneOf:
- type: integer
- type: object
|
This can get tricky because it recursive, so you can't just look at the direct subschema. Example: A:
allOf:
- $ref: '#/components/schemas/B'
B:
allOf:
- $ref: '#/components/schemas/C'
C:
allOf:
- $ref: '#/components/schemas/D' Also, if you are looking for corner cases, here is one that you probably won't see very often :-) Model:
anyOf:
- allOf:
- type: string
maxLength: 15
- type: boolean
- $ref: '#/components/schemas/D'
allOf:
- type: string
minLength: 10
- pattern: [a-z]{3,50}
oneOf:
- type: string This is a complicated way to specify the value must be a string between length 10 and 15, and all characters must be lowercase a-z, with possibly some constraints coming from |
49de85e
to
267e090
Compare
modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java
Show resolved
Hide resolved
This reverts commit f88dcb13594b3e21b734b8b1569ca29bcbf9cb13.
1550753
to
cdf4d35
Compare
Small offtopic. This PR should be merged as temporarily solution from my point of view. I think that we'll end up with parsing spec file directly anyway. It's hard to store all spec data in codegen variables if it's even possible(I don't know how to store deeply nested values in mustache variables). Maybe server framework should be able to parse full spec( For instance we have stale issue #4513 which cannot be solved because of codegen |
@wing328 or @jimschubert could you check and merge? It's 3 months already. |
@ybelenko this has been open for 3 months, but you have commits up to 15 days ago. Realize when you make additional changes and force-push, things have to be reviewed again. If @sebastien-rosset is content with your recent changes, I'm fine to have this merged. |
Sure, but I didn't drop any commit. I rebase and force push to:
I've requested your review again at June 13th. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-approving but awaiting feedback from others whether their concerns have been addressed.
@ybelenko just curious, why was rebase necessary rather than merging master up to your branch? The rebase and force-push will often make inline review comments and their resolutions no longer accessible. It also makes it so that contributors can't tell whether this was just a master sync or if there was new work that requires another review. Reviewing each of your 4 force pushes, I now see there were no changes since Jun 9, but it's unlikely that any contributors are going to dig into force push history when re-reviewing a pull request. My question still remains that if your last true change was June 9, and concerns were still voiced on June 10, whether or not those concerns are blockers before merge. |
I've been in a project where
Beside, it was the first time I've changed my solution completely in the middle of PR. I agree that changes was hard to review. My bad.
I don't know whether my fix solves @dang-gyg issue because there is no details. I think that @sebastien-rosset concerns solved because I decided to fix it inside |
modules/openapi-generator/src/test/resources/3_0/6150_model_json_inline.yaml
Show resolved
Hide resolved
modules/openapi-generator/src/test/resources/3_0/6150_model_json_inline.yaml
Show resolved
Hide resolved
modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java
Show resolved
Hide resolved
The changes look good. I did make a couple of suggestions to add code comments. This is to avoid the element of surprise/confusion when future code maintainers will read the source code. It's not immediately obvious the |
Thanks @sebastien-rosset. I'll add those comments and push. Then I'll call everybody to check again 😄 |
Obviously CI failure isn't related to changes, because I modified comments only. |
thanks! lgtm |
CircleCI was a timeout I have the type/format change and 20 other attributes were weren't copying in #7106, so I'll merge this and retain your comments when I merge it up into my PR. |
* master: [core] Add type and format properties to model of inline response (#6153) [PowerShell] better publishing workflow (#7114) [aspnetcore] Typo issues in docs and generated code (#7094) fix http signaure auth in build.sbt (#7110) fix for the issue facing spec invlolving arrayschema structure with ref (#6310) [csharp-netcore] added cancellation tokens to async calls (#7077) [PS] Allow CI to publish the module (#7091) [Dart] Treat float as double (#6924) [Java][jersey2]Fix gradle HttpSignatureAuth dependencies (#7096) move maven,gradle tests to shipppable ci (#7108)
* master: (129 commits) [typescript-axios] add promise to bearer and oauth tokens (#7132) update doc [REQ] Added enumClassPrefix option to Go server generation (#7008) [Java][jersey2] Add helper methods for oneOf Java classes (#7115) [Kotlin][Retrofit2] fix missing import for file (#7121) adding handling for epoch dates in javascript ApiClient mustache file (#6497) (#6504) update doc comment out cpanm in travis [Kotlin] Rxjava3 support (#6998) [BUG][JAVA] Fix error handling in okhttp-gson async api client (#7089) Update to reset httpRepsonse.Body (#6948) [php-lumen] Set required PHP version to ^7.2.5 (#6949) [contrib][docs] Assert importance of title/description/repro steps (#7103) ISSUE-4222: Prevent conflicts with accept(s) local variables in generated Java RestTemplate ApiClient (#7101) [bug][core] Copy all attributes (not properties) on composed schemas when flattening models (#7106) [core] Add type and format properties to model of inline response (#6153) [PowerShell] better publishing workflow (#7114) [aspnetcore] Typo issues in docs and generated code (#7094) fix http signaure auth in build.sbt (#7110) fix for the issue facing spec invlolving arrayschema structure with ref (#6310) ...
I tried to copy original
Schema
instance, but failed I guess. Need a small help from a core team.Closes #6150
PR checklist
./bin/
(or Windows batch scripts under.\bin\windows
) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run./bin/{LANG}-petstore.sh
,./bin/openapi3/{LANG}-petstore.sh
if updating the code or mustache templates for a language ({LANG}
) (e.g. php, ruby, python, etc).master
,4.3.x
,5.0.x
. Default:master
.