Skip to content
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

[Java/Core] Adds properties to ComposedSchema models when they exist #4482

Merged
merged 2 commits into from
Dec 4, 2019

Conversation

spacether
Copy link
Contributor

@spacether spacether commented Nov 13, 2019

This PR fixes this issue:
#4515

Currently, when we process composedSchemas, we are not including any properties that are described in the properties section of that model's schema. An example is the below fruit model, which leaves out the color property when we ingest it and make a model.

openapi: 3.0.1
info:
   title: fruity
   version: 0.0.1
paths:
   /:
      get:
         responses:
            '200':
               description: desc
               content:
                  application/json:
                     schema:
                        $ref: '#/components/schemas/fruit'
components:
   schemas:
      fruit:
         title: fruit
         properties:
            color:
               type: string
         oneOf:
            - $ref: '#/components/schemas/apple'
            - $ref: '#/components/schemas/banana'
resolved
      apple:
         title: apple
         type: object
         properties:
            kind:
               type: string
      banana:
         title: banana
         type: object
         properties:
            count:
               type: number

This spec passes validation at: https://apitools.dev/swagger-parser/online/#

This PR fixes this issue, adding properties to the composed model.
We also add a test in DefaultCodegenTest.java where we check that the fruit model contains the color property.

This issue was also recently noticed and fixed in swagger-codegen at: swagger-api/swagger-codegen#9827

Note: additional properties should also be allowed in composed schemas, but that feature is blocked by an upstream bug in swagger-parser. I've included a commented out additionalproperties property in the fruit spec, commented out code to process it in DefaultCodegen.java. Data in both locations links to the open swagger-parser issue which I created at: swagger-api/swagger-parser#1252

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./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).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

Java Technical Committee
@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10)

Core Team
@wing328 (2015/07) ❤️
@jimschubert (2016/05) ❤️
@cbornet (2016/05)
@ackintosh (2018/02) ❤️
@jmini (2018/04) ❤️
@etherealjoy (2019/06)

@spacether spacether changed the title Adds properties to ComposedSchema models when they exist [Java] Adds properties to ComposedSchema models when they exist Nov 13, 2019
@spacether spacether mentioned this pull request Nov 17, 2019
5 tasks
Copy link
Contributor

@lwlee2608 lwlee2608 left a comment

Choose a reason for hiding this comment

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

Looks good to me. It is is identical with my PR #4516 . I shall cancel my PR since this is done earlier.

I believe you should cc the core member instead of java technical committee.

@spacether spacether changed the title [Java] Adds properties to ComposedSchema models when they exist [Java/Core] Adds properties to ComposedSchema models when they exist Nov 18, 2019
@spacether
Copy link
Contributor Author

Looks good to me. It is is identical with my PR #4515. I shall cancel my PR since this is done earlier.

I believe you should cc the core member instead of java technical committee.

Ah, didn't know that. I just cced them, thanks!

@lwlee2608 lwlee2608 mentioned this pull request Nov 18, 2019
5 tasks
@spacether
Copy link
Contributor Author

spacether commented Nov 22, 2019

Core Team: @wing328 @jimschubert @cbornet @ackintosh @jmini @etherealjoy
Gentle reminder that this still needs a review

Copy link
Contributor

@jeff9finger jeff9finger left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@spacether
Copy link
Contributor Author

@wing328 these changes were approved 8 days ago. Is there a milestone that they can be added to? Is any more work required in the PR?

Copy link
Member

@jimschubert jimschubert left a comment

Choose a reason for hiding this comment

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

Awesome.

Comment on lines +25 to +27
# additionalProperties:
# type: string
# uncomment this when https://github.com/swagger-api/swagger-parser/issues/1252 is resolved
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't imagine this will get fixed anytime soon in swagger-parser for the 3.0 specification (the revision versions 3.0.1/3.0.2 are supposedly documentation-only changes).

The behavior you're seeing where this returns MapSchema is defined behavior: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#model-with-mapdictionary-properties

The requested behavior is undefined, as there's not enough clarity in the specification around constraints of using oneOf. It should be that oneOf can only coexist with discriminator.

oneOf is taken from JSON Schema's specification Section 9.2.1.3, which says:

This keyword's value MUST be a non-empty array. Each item of the array MUST be a valid JSON Schema.

An instance validates successfully against this keyword if it validates successfully against exactly one schema defined by this keyword's value.

Meaning your example is only a valid object if it is exactly apple or exactly banana, and no dynamic properties via additionalProperties are allowed due to the use of MUST rather than SHOULD in the JSON Schema specification.

The discriminator support for oneOf is a JSON Schema extension defined in the OAS Composition and Inheritance (Polymorphism) section.

Ideally, a spec with oneOf any anything besides discriminator should fail validation. We could consider adding this strict validation to OpenAPI Generator, or at least as a recommendation via the --recommend flag to the validate command.

addVars(m, unaliasPropertySchema(schema.getProperties()), schema.getRequired(), null, null);
}

// uncomment this when https://github.com/swagger-api/swagger-parser/issues/1252 is resolved
Copy link
Member

Choose a reason for hiding this comment

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

See my comment on the example spec document in this PR.

@@ -1787,6 +1787,17 @@ public CodegenModel fromModel(String name, Schema schema) {
Map<String, Schema> allProperties = new LinkedHashMap<String, Schema>();
List<String> allRequired = new ArrayList<String>();

// if schema has properties outside of allOf/oneOf/anyOf also add them to m
if (schema.getProperties() != null) {
addVars(m, unaliasPropertySchema(schema.getProperties()), schema.getRequired(), null, null);
Copy link
Member

Choose a reason for hiding this comment

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

@wing328 See my other comment in this PR. Constraints around structures containing oneOf aren't very clearly defined in OAS 3.x, since the extension to include discriminator makes it look like any Schema properties are valid.

Do you think we should log a warning here, or wrap it the a ! strict spec check, or both? I've commented elsewhere as well that we might want to add to the validate and/or recommends about this.

@jimschubert
Copy link
Member

This spec passes validation at: https://apitools.dev/swagger-parser/online/#

Just a heads up that our CLI also validates:

openapi-generator validate -i openapi.yml

@jimschubert jimschubert merged commit c882338 into OpenAPITools:master Dec 4, 2019
@spacether spacether deleted the fixes_oneof_class branch December 4, 2019 03:01
@spacether spacether restored the fixes_oneof_class branch February 3, 2020 17:48
@spacether spacether deleted the fixes_oneof_class branch April 30, 2020 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants