-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[python-client] Add model default values #1776
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
[python-client] Add model default values #1776
Conversation
dcb9195 to
5b663a1
Compare
| // include fallback to example, default defined as server only | ||
| // example is not defined as server only | ||
| if (p.getExample() != null) { | ||
| if (p.getExample().toString().equalsIgnoreCase("false")) |
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.
What about using Boolean.valueOf instead of using equalsIgnoreCase?
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.
Boolean.valueOf(string) will always return True or False,
so Boolean.valueOf("5") would return False.
When I pass in "5" as a boolean the code should return null. Code updated use equalsIgnoreCase to check for "true" and "false" otherwise return null.
I'd prefer matching on true false strings rather than truthy "0" "5" "1.234" "[1,2,3]" because truthiness differs from language.
equalsIgnoreCase lets us only look for a string match to true or false ignoring case.
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.
When I pass in "5" as a boolean the code should return null
If someone puts down 5 as the example value and assume it will be treated false in boolean, it's the user's issue (not something we need to handle) and I don't think it should return null (clearly there's a value defined)
We've seen spec using 1 or 0 to as example value for boolean type (not entirely correct as they should be using "true" or "false" instead) but still we want to able to handle these cases correctly as some languages (e.g. Perl) treat 1 as true.
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.
Coercing it to a boolean gives the user back some boolean which is usefull. I will change it to Boolean.valueOf. This behavior may surprise python users though because they will be using a python client with java casting of string truthiness.
"1" -> True in Python, false in Java
"0" -> True in Python, false in Java
|
@spacether the change looks good to me. Thanks for your contribution. |
* master: (52 commits) Support for "x-enum-descriptions" (OpenAPITools#1752) add new sample files Add a checklist to issue report (OpenAPITools#1851) Fix typo in template (OpenAPITools#1859) update samples add isModel to updatebooleanflagwithcodegenproperty (OpenAPITools#1844) [python-client] Add model default values (OpenAPITools#1776) fix: force to decode as utf-8 when header contains application/json to avoid text garbling. (OpenAPITools#1700) Better support for composed schema (allOf) (OpenAPITools#1842) Add additional properties to Java CodegenModel (OpenAPITools#1854) Minor Angular type improvements (OpenAPITools#1843) [DART] fix: set fields to null if json value is null. (OpenAPITools#1798) add options to maven plugin (OpenAPITools#1845) fix unqiue name in handling forward slash (OpenAPITools#1839) better handle of oauth (OpenAPITools#1838) [C] Support for authentication methods (OpenAPITools#1628) better error message when parameter ref not defined (OpenAPITools#1837) Add links to articles about openapi generator Add a link to @watiko article Delete langs command (OpenAPITools#1836) ...
|
@spacether If the default value is not defined, it shouldn't fall back to the example value. I'll file a PR to remove the fallback. |
|
Good to know. Thank you you @wing328 sorry for the breaking change. |
|
@spacether no worries. In the templates, you can do something similar to the following to have the default value fall back to example value (not something I recommend but ok if there's a valid use case) |
|
FYI. Filed #2213 |
* Adds two models to the v2.0 spec, uses examples as defaults in python client * Adds array default and type_holder_default and type_holder_example tests * Re-generated python security client with ./bin/security/python-petstore.sh * Changes comment text, rebased master * Updates client + server samples * Adds missing samples updates * Changes python client to look for true or false with booleans in toDefaultValue * Changes boolean casting to use Boolean.valueOf * Adds deserialization fix for python tests * Changes Mock to namedtuple in python deserialization tests * Actually remove unittest.mock
PR checklist
./bin/to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/python-petstore.shand./bin/security/python-petstore.shif updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\.master,. Default:3.4.x,4.0.xmaster.Description of the PR
This PR lets python client users set model defaults on integers, numbers, and arrays when they are not currently able to in a swagger2.0 spec. The new code loads model variable examples into the model variable default value if the default is not already set and an example exists for the variable.
Fixes this issue: #1748
Reasons to do this:
The current swagger-parser is not including default values that are float, integer, or array when processing a swagger2.0 spec. So in a swagger 2.0 spec, there is no way to set defaults on:
This PR adds in fallback code which uses and assigns a spec example value as the default value if one exists in the spec.
So it uses examples to set defaults on the types
Tests