-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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] Changes to the templates for test code of python client. #4514
Conversation
@michelealbano can you copy the members of the python technical committee from here? |
Like this? |
What should I do to support the revision process of the code? |
modules/openapi-generator/src/main/resources/python/api_test.mustache
Outdated
Show resolved
Hide resolved
@@ -27,8 +39,14 @@ class {{#operations}}Test{{classname}}(unittest.TestCase): | |||
{{#summary}} | |||
{{{summary}}} # noqa: E501 | |||
{{/summary}} | |||
|
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.
Why not use code in here?
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/api_doc_example.mustache
If you separated api_doc_example.mustache into:
- api_doc_example_imports.mustache
- api_doc_example_code.mustache
Then you could use api_doc_example_code.mustache here
and use the same mustache file in two places
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.
Nice idea. Can you provide an example? I did not work on the documentation up to now.
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 is an example where a mustache file imports other mustache files into itself
https://github.com/spacether/openapi-generator/blob/allof_anyof_oneof_addition/modules/openapi-generator/src/main/resources/python/python-experimental/model_utils.mustache#L59
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.
As you suggested in the comment above, I will focus on the models, and I will correct this in a future PR.
modules/openapi-generator/src/main/resources/python/model_test.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/python/model_test.mustache
Outdated
Show resolved
Hide resolved
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.
Thanks for your PR! Please address the open comment issues.
@@ -5,14 +5,21 @@ | |||
from __future__ import absolute_import | |||
|
|||
import unittest | |||
import datetime |
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.
Please delete some of the existing model tests and have your template generate new examples. How about replacing the below tests:
- samples/client/petstore/python/test/test_animal.py (discriminator model)
- samples/client/petstore/python/test/test_cat.py (composed allof model)
- samples/client/petstore/python/test/test_enum_class.py (model with enums)
- samples/client/petstore/python/test/test_format_test.py (normal model with many parameter types)
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 will have no time for that.
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.
The request up above will take very little time.
To do it one would run:
rm samples/client/petstore/python/test/test_animal.py
rm samples/client/petstore/python/test/test_cat.py
rm samples/client/petstore/python/test/test_enum_class.py
rm samples/client/petstore/python/test/test_format_test.py
bin/python-petstore.sh.sh
# turn on the test server
docker run -d -e SWAGGER_HOST=http://petstore.swagger.io -e SWAGGER_BASE_PATH=/v2 -p 80:8080 swaggerapi/petstore
# test the updated client
cd samples/client/petstore/python
# run the tests
make test
That should take around a minute max.
These api_test and model_test updates will impact all of the tests that are auto generated when the users run the generator.
This pull request does not yet include any tests of this new functionality.
Tests will need to be added showing that both the new model tests and the api client tests produce code which runs and passes tests.
If these tests were omitted this update could breaking the tests produced in the python client. If this is too much work for this PR the model_test update and the api_test update can be broken up in to different PRs.
I am hearing that you are busy. We can wait until you have time to add this test verification. It is very important that new changes do not break the existing tests, so we can go at whatever speed works for you .
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.
Another thing that we can do is look at the content of model_test.mustache in other generators and see how they handle instantiating parameters that have model values. Maybe they have some cool tricks that we can use.
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.
Hi! I tried to remove and re-create all the existing samples/client/petstore/python/test/*
, and the hardest tests to manage are exactly the ones with discriminators, since python and python-experimental produce different signatures. For example, for petstore/models/Cat.py:
python: def __init__(self, declawed=None, local_vars_configuration=None):
python-experimental: def __init__(self, class_name, _check_type=True, _from_server=False, _path_to_item=(), _configuration=None, **kwargs):
The funny thing is that class_name (used in the discriminator) is NOT in the old generator, and it is required (it has no default) in the python-experimental generator.
The only solution I see is to develop a model_test.moustache for the old python, and a model_test_experimental.moustache for the python-experimental.
Any suggestions on alternative solutions?
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.
No, I mean when generating the code. For example:
Tiger:
type: object
required:
- teeth
- cat
properties:
teeth:
type: string
cat:
$ref: '#/definitions/Cat'
Cat:
type: object
required:
- fur
properties:
fur:
type: string
animal:
$ref: '#/definitions/Animal'
Animal:
type: object
required:
- color
properties:
color:
type: string
What do you do, when generating the make_instance for Tiger? The options are to generate:
return Tiger(
teeth = 'many',
cat = openapi_client.models.category.Cat(
fur = 'short',
animal = openapi_client.models.category.Animal(
color = 'black and yellor',
)))
or
return Tiger(
teeth = 'many',
cat = openapi_client.models.category.Cat(
fur = 'short',
))
Ideally, you would generate the first when include_optional is True and the second then include_optional is False. Anyway, the code for the Cat is generated by toExampleValue, which does NOT know what include_optional is. Thus, internal properties are either generated ALWAYS with all optional parameters, or NEVER with optional parameters.
And now, here is the bomb:
TagList:
type: object
required:
- name
properties:
name:
type: string
nextTag:
$ref: '#/definitions/TagList'
It is a linked list, and it should have an empty nextTag sooner or later. Anyway, if we always comprise optional parameters in toExampleValue, we will have infinite recursion.
So, we should NEVER add optional parameters at toExampleValue level. Do you agree?
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.
So I see two solutions to these problems.
For the problem of needing to pick whether optional is required or not
Create a string property in CodegenProperty called exampleOptional. In it store the example where include_optional is true. In example store the example where include_optional is false
For the problem of a model with params or items of type model, you can include a list of Schemas or dataTypes that you are inspecting in your toExample function. Every time you call it recursively, add a schema. If we are going to add a schema that we have already added, add it then return, which will end our recursive calls.
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.
The first solution implies changing the data model, and I prefer to leave it to the next developer.
I have implement the second solution.
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 am hearing that you don't want to edit the CodegenProperty.java class.
For the first solution we can support deep include_optional=True without changing the data model.
This would work by storing the exampleOptional string value in a property that we are not using in CodegenProperty.
Why not store that value in the parameter.unescapedDescription property and add a comment describing what we are storing in it?
If this is something that you still don't want to do that's fine, just let me know. I just want you to know that there is another viable solution here.
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.
Yep, I would prefer to have this PR accepted, then do something for the api, then I could come back to this issue - after discussing it with you in a chat.
modules/openapi-generator/src/main/resources/python/api_test.mustache
Outdated
Show resolved
Hide resolved
Hi, I cannot understand why the tests fail. Can you help? |
So one test is failing because of some go issue. Another test is failing because of a connexion library issue. Can you rebase your branch on the master branch? |
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java
Outdated
Show resolved
Hide resolved
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/python/api.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/python/api_client.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/python/model.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/python/model_test.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/python/model_test.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/python/model_test.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/python/python-experimental/model.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/python/python-experimental/model_test.mustache
Outdated
Show resolved
Hide resolved
samples/client/petstore/python-experimental/dev-requirements.txt
Outdated
Show resolved
Hide resolved
samples/client/petstore/python-experimental/test/test_additional_properties_any_type.py
Outdated
Show resolved
Hide resolved
samples/client/petstore/python-experimental/test_python2_and_3.sh
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/python/python-experimental/api_test.mustache
Outdated
Show resolved
Hide resolved
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java
Outdated
Show resolved
Hide resolved
#5045 only changes maven repo declarations to match what is documented on Gradle's site. Current master contains all fixes for the maven http issues. If you merge master into this branch, those issues should be gone. |
bd73895
to
2ce20b2
Compare
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java
Outdated
Show resolved
Hide resolved
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java
Outdated
Show resolved
Hide resolved
2ce20b2
to
010f2aa
Compare
...erator/src/main/java/org/openapitools/codegen/languages/PythonClientExperimentalCodegen.java
Outdated
Show resolved
Hide resolved
Ok. Problem with version control. Done |
010f2aa
to
4cc3813
Compare
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.
@michelealbano the http url needs to be reverted and the init file removal in pythonclient needs to be reverted. You mentioned that you wouldn't be available for a bit. I could
- wait for you to make the fixes amd approve after or
- you can add me as a contributor and I can make the update and request an approval from the core team when you are busy
Either works. Please let me know what you prefer.
Please elaborate on #4514 (review)
|
...erator/src/main/java/org/openapitools/codegen/languages/PythonClientExperimentalCodegen.java
Outdated
Show resolved
Hide resolved
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.
@michelealbano I added comments in the specific remaining locations
Tests for python client, comprising support for additional_properties and arrays. There are ugly workarounds for when there are discriminators, since the python client generator does not fully handle them. Cool indentation of test files.
4cc3813
to
753fa6c
Compare
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.
@michelealbano thank you for all of your hard work on this!
This looks good.
Is there anything else that needs to be done to end this PR? |
@michelealbano the PR looks good to me and I've just merged it. @spacether thanks for reviewing the change. |
Just a heads up, this change may have caused a regression in master. Once we have more details, the change may need to be reverted and re-opened against the 4.3.x branch. I'd recommend future changes for this work, regardless of whether it's temporarily reverted:
cc @spacether |
@jimschubert this is a PR on the python generator.
|
@spacether it Looks like a NPE which would've probably been caught if the new method was unit tested. I've opened #5082, but won't have time to verify it until probably later tonight. |
Good point |
Generation of test code for the python client, for everything "required":
I am not sure about the implementation for "binary", but everything else should be fine. Please provide comments and I will improve my contribution.
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
.