-
-
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
[BUG][MULTIPLE] 3.1.0 spec, additionalProperties: false with nullable properties generates invalid models #20213
Comments
if you down grade the spec to 3.0.1, did you get the same issue? just want to know if the issue only occurs with 3.1.0 spec. |
Under 3.0.1, the generated models behave as I would expect: no unnecessary I do remember us needing some other feature unique since 3.1.0 so downgrading is likely not an option for us, although I can't recall off the top of my head what it was. |
Going by the discussion on OAI/OpenAPI-Specification#3148, which matches what I've found elsewhere but decently summarizes it, nullable properties in a 3.1 specification can be one of the following openapi: '3.1.0'
components:
schemas:
Foo:
type: 'object'
properties:
bar:
type: ['string', 'null']
# This style still allows us to set other properties only valid for the 'string' type
format: 'uri'
Bar:
type: 'object'
properties:
bar:
oneOf:
- type: 'string'
format: 'uri'
- type: 'null' As a comparison, in 3.0, it would be done with an explicit User:
type: object
properties:
address:
type: object
nullable: true
properties:
city:
type: string The above is from one of the tests' examples in this project. Now, I think the easiest way to handle this would be to modify the default generator (assuming that's where this is parsed - but I haven't gotten to the implementation stage of a fix yet) to:
I expect that the alternative would be to consider both @wing328, I'm assuming you're the right person to ask here - does this proposed behaviour sound acceptable? If so, I can make an attempt for a PR. |
Also - it seems that the example openapi validator in the Github issue template is no longer a valid URL. |
Actually, it seems support for this has been in the project for a long time already: Line 1737 in a447b59
Given that existing code, I suppose that implementing this same logic for Java would be necessary to support nullable types in 3.1... But I expect the original issue I reported to not be resolved by just that. |
Bug Report Checklist
Description
For our OpenAPI spec using the 3.1.0 specification, we make heavy use of the
additionalProperties: false
keyword on most of our objects to make validation stricter, both while validating our examples and in the generated server code. By adding they keyword, we ensure we don't get false negatives while validating examples with non-required properties with a typo in the key, and make issues in usage of the APIs clearer.However, when this keyword is combined with an object with nullable properties, the generated models are invalid (see the example below). I've tested both the Typescript-Angular client generator, the Java OKHTTP client generator and the Java spring-boot API spec generator, and each of them shows the same behaviour: the nullable property is inlined to a new, invalid object resulting in broken code.
openapi-generator version
Tested with version 7.9.0 and 7.10.0, both show the same behaviour.
OpenAPI declaration file content or url
Generation Details
Verified with the following configs, and no further settings. Run with
java -jar openapi-generator-cli-7.10.0.jar generate --config <config-file-as-shown-in-examples>.yaml
and no further customization. I will add additional samples in comments for other generators.Spring Generator
Generated code below. Note the reference objects to show the expected behaviour:
Note that I've also tried the config below, which seems to make practically no difference - except that the incorrectly introduced wrapper object also gets additionalProperties if set to false:
Resulting in the following
BrokenObjectSomeString.java
:Steps to reproduce
Create a file with the contents as above - or just any OpenAPI 3.1.0 spec where an object has both
additionalProperties: false
and any nullable field, done by adding aoneOf
oranyOf
where one option is the actual type of the field, and the other is{ "type": "null" }
.Run a generate task with any of the mentioned generators (likely others as well).
Related issues/PRs
None that I can find. Can supply one if I find a solution.
Suggest a fix
Researching one, though I already want to have the issue filed for reference.
The text was updated successfully, but these errors were encountered: