Repository navigation
Description
Description
I'm modeling an existing api in an OpenAPI 3.0 spec.
A key thing across most of this particular API (like it or not) is that all fields must be present (they are all required), but most of the time the fields are nullable. Some APIs include the fields and never use them (ie they are always set to null), but try to send a request without the field and it fails. (A replacement API is in the works, but until then, legacy prevails...)
So, I tried to model this by listing the requestBody parameters, and adding all of them to required. Then, I marked the fields that can be null as nullable: true.
However, in the python model template, required results in:
openapi-generator/modules/openapi-generator/src/main/resources/python/model.mustache
Lines 97 to 100 in 7184f1e
| {{#required}} | |
| if {{name}} is None: | |
| raise ValueError("Invalid value for `{{name}}`, must not be `None`") # noqa: E501 | |
| {{/required}} |
Could this be updated to support nullable? So that it raises the ValueError on None only if required and NOT nullable?
openapi-generator version
master
OpenAPI declaration file content or url
components:
schemas:
MySomethingOrOther:
type: object
required:
- id
- required_nullable_option
properties:
id:
type: integer
example: 96
required_nullable_option:
type: string
nullable: true
example: "something"Command line used for generation
python is the target
Steps to reproduce
Mark something as both required and nullable, and then pass None as the value.
Suggest a fix/enhancement
check for required and not nullable
openapi-generator/modules/openapi-generator/src/main/resources/python/model.mustache
Lines 97 to 100 in 7184f1e
| {{#required}} | |
| if {{name}} is None: | |
| raise ValueError("Invalid value for `{{name}}`, must not be `None`") # noqa: E501 | |
| {{/required}} |
I'm not familiar with what sets the vars like "required" in the template, so I'm not sure where to get the nullable option to modify this.
Activity
wing328 commentedon Jul 26, 2018
@cognifloyd yes, may I know if you've time to contribute the enhancement? I can show you some good starting points.
cognifloyd commentedon Aug 8, 2018
I looked to see if nullable was set injected into the templates, but it's not. A couple of the templates use
x-nullable, which gets passed in thanks to the vendorx-prefix. I don't have time or inclination to deal with the Java side of this. If someone wants to add injecting nullable into templates, then I'd be happy--assuming I can find some time--to take care of the mustache template + python portion.cognifloyd commentedon Oct 13, 2018
I found the inclination and so went digging through the Java even though I'm a rookie with Java. I started with swagger-codegen (because I'm using it via swaggerhub right now and I'm getting irritated with OAS 2.0) and then came to do the same here and discovered that the Java side is done.
And go figure. I didn't know what I was doing in swagger-codegen, so I updated (squash + force push) my PRs there based on your excellent work here.
Now, I hope I can find some more time to actually work on the python side.
wing328 commentedon Oct 13, 2018
@cognifloyd I've submitted #1073 before. Is that what you're looking for?
(sorry that I forgot to update you about this enhancement before)
cognifloyd commentedon Oct 13, 2018
Wow. yeah. That's great! 🎉 Thank you @wing328 for doing that. Now to play with and use it.
wing328 commentedon Oct 13, 2018
@cognifloyd you're welcome. Please give it a try and let us know if you've any feedback.
sanderegg commentedon Nov 7, 2018
it seems to be again a problem since release 3.3.2
wing328 commentedon Nov 7, 2018
@sanderegg can you please elaborate on the issue with a spec to reproduce the problem and the actual vs expected output?
sanderegg commentedon Nov 7, 2018
@wing328 sorry yes, actually the exact same case described in here.
I was happily using version 3.2.3 till now and moved to 3.3.2 and found out that a required and nullable field would now throw a ValueError from the generated python code.
The generated code for this part now creates:
And it is not the case with version 3.3.1
wing328 commentedon Nov 7, 2018
@cognifloyd thanks for sharing the spec. Are you using OpenAPI spec v2 or v3?
Only v3 officially supports
nullable. For v2, you can usex-nullable(vendor extension) instead.wing328 commentedon Nov 7, 2018
I also did a test locally and it's working fine.
sanderegg commentedon Nov 7, 2018
@wing328 using spec v3

here is a screenshot of the differences I see between 3.3.1 and 3.3.2 in the python generated code:
left is 3.3.1, right is 3.3.2 for the same specs
wing328 commentedon Nov 7, 2018
@cognifloyd I was not able to repeat the issue with OAS3 spec using the latest master. The nullable support was introduced to v3.3.0 via #1073.
Can you please try the latest master instead? You can find the links to the SNAPSHOT version in the project's README.
cognifloyd commentedon Nov 7, 2018
@wing328 I think you meant @sanderegg :)
sanderegg commentedon Nov 7, 2018
I'm using the docker images, is there one for master as well? like latest? it seems to be recent.
sanderegg commentedon Nov 7, 2018
well using the latest docker (which is an hour old at this time of writing) I get the same issue.
wing328 commentedon Nov 7, 2018
@cognifloyd sorry :(
@sanderegg can you please share the full spec with me so that I can repeat the issue locally? You can find my email address in my Github profile page.
sanderegg commentedon Nov 7, 2018
yes will do in the evening... going home at the moment...
sanderegg commentedon Nov 7, 2018
Ok I think I found the issue. It seems the problem arises when the property is nullable and a ref (see schema that reproduces the issue below). I confirm that the problem with nullable of type string works fine. The problem arises when the property is a ref and nullable.
Now searching a bit, I am not quite sure if this is accepted as such or if one should use some allOf trick to make that work? What I am sure is that the very same spec did work until the latest release...
sanderegg commentedon Nov 7, 2018
so replacing the above specs, by
works!
Thanks for your time, I think it's all good! probably made my api better;)