[Python] improved class reflection in deserialization and better model construction with mandatory attributes #5512

Merged
merged 8 commits into from May 17, 2017

Conversation

Projects
None yet
3 participants
Contributor

easz commented Apr 29, 2017 edited

PR checklist

  • Read the contribution guidelines.
  • Ran the shell/batch script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)
  • Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Description of the PR

(details of the change, additional tests that have been done, reference to the issue for tracking, etc)

try to improve python client generation.

  • model construction with mandatory attributes
  • improve class reflection in deserialization
  • test (top-level) enum class deserialization

See
#5375
#5421

easz added some commits Apr 28, 2017

@easz easz ignore .vscode ca45e6f
@easz easz fixed test case for models requiring mandatory attributes. added dese…
…rialize test for enum class
5b5c2f6
@easz easz construct model with mandatory attributes. improve class reflection (…
…e.g. deserialize). disable generated empty model unit test for further FIXME
5bb87a2
@easz easz rebuilt samples 89d8bd4
@easz easz FIXME comment
c40b7dc
@easz easz FIXME comment
8b584d2
@easz easz fix deserialization enum test
c64c7ad
@easz easz Merge branch 'master' of https://github.com/swagger-api/swagger-codegen
… into python-improved
c01cb9e

wing328 added this to the v2.2.3 milestone May 1, 2017

Member

wing328 commented May 1, 2017

Thanks for the PR.

cc @frol @scottrw93 @taxpon @baartosz

@easz btw, have you checked out the 2.3.0 branch in which Python API client has a lot of enhancements (with breaking changes)?

Contributor

easz commented May 2, 2017

$ cd swagger-codegen/

$ git checkout remotes/swagger-api/2.3.0

// the last commit c01cb9e of this PR is not part of changes
$ git cherry-pick ca45e6f 5b5c2f6 5bb87a2 89d8bd4 c40b7dc 8b584d2 c64c7ad

// delete 'test' for new code
// the 'test' folder will not be overwritten if already exist. Desired behavior?
$ rm -rf samples/client/petstore/python/test

$ ./bin/python-petstore.sh

$ cd samples/client/petstore/python/
$ ./test_python2_and_3.sh

Ran 100 tests in 11.110s

OK (SKIP=1)
________________________________________________________ summary ________________________________________________________
py27: commands succeeded
py3: commands succeeded
congratulations :)

Member

wing328 commented May 2, 2017

// the 'test' folder will not be overwritten if already exist. Desired behavior?

Yes, that's the expected behaviour because we do not want to overwrite the test files as the users might have manually added a few test cases in the test files.

Contributor

easz commented May 2, 2017

that means
./bin/python-petstore.sh will never update 'test' folder even 'model_test.mustache' is changed?

actually I did assume 'test' folder is automated generated tests
and 'tests' folder has user defined tests.

Member

wing328 commented May 2, 2017

./bin/python-petstore.sh will never update 'test' folder even 'model_test.mustache' is changed?

Yes, that's the current behaviour.

actually I did assume 'test' folder is automated generated tests
and 'tests' folder has user defined tests.

That's one way to do it.

We plan to leverage .swagger-codegen-ignore to control which files not be overwritten during code generation.

Member

wing328 commented May 8, 2017

@easz if I understand correctly, these changes are backward-compatible with the existing auto-genreated Python API using the latest master.

I'll merge this PR into master on coming Wed if no one has any further feedback.

@@ -23,6 +23,7 @@ packages/
.pub
.packages
.vagrant/
+.vscode/
@frol

frol May 8, 2017

Contributor

I think, this should be added locally on your own machine to the global git ignore file: https://davidwalsh.name/global-gitignore

Contributor

easz commented May 8, 2017 edited

yes. the change (e.g. required attributes need to be defined when calling model constructor) is backward-compatible with the existing auto-genreated Python API.

but some test cases has been changed to compatible with the change.

and any existing client code by any user may break if they did not assign required attributes in the constructor.

Contributor

frol commented May 8, 2017

The change looks great to me.

P.S. I have noticed that there are some places where 2 spaces are used for indentation while 4 spaces indentation is recommended. Is this worth to update in the scope of this PR?

Member

wing328 commented May 9, 2017

Is this worth to update in the scope of this PR?

I don't think so. I'll create another issue to track the code format of Python API client similar to what we've done for the PHP API client (refactored): #5277

wing328 added the Issue: Bug label May 16, 2017

Member

wing328 commented May 17, 2017 edited

and any existing client code by any user may break if they did not assign required attributes in the constructor.

@easz It's definitely the correct behaviour and I would imagine some applications using the upgraded Python API client will throw exception.

May I know what the exception message looks like? If it's clear, I think it's fine for this enhancement to go into the current master.

Contributor

easz commented May 17, 2017

a ValueError, if a required attribute is not given in a model constructor

Exception is thrown from files generated by model.mustache

  if {{name}} is None:
    raise ValueError("Invalid value for `{{name}}`, must not be `None`")

before the change, the test cases under test/ just construct every possible model with null content

Member

wing328 commented May 17, 2017

@easz the error message looks good to me. We might revise it later to make it consistent with the error message shown by other API clients.

wing328 merged commit a6b7f60 into swagger-api:master May 17, 2017

3 checks passed

Shippable Run 3283 status is SUCCESS.
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

wing328 changed the title from Python improved to [Python] improved class reflection in deserialization and better model construction with mandatory attributes May 17, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment