Skip to content

Fix multipart File fields #372

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

Merged
merged 10 commits into from
Jun 2, 2021

Conversation

csymeonides-mf
Copy link
Contributor

No description provided.

@codecov
Copy link

codecov bot commented Mar 31, 2021

Codecov Report

❗ No coverage uploaded for pull request base (main@67db1a1). Click here to learn what that means.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##             main      #372   +/-   ##
========================================
  Coverage        ?   100.00%           
========================================
  Files           ?        47           
  Lines           ?      1558           
  Branches        ?         0           
========================================
  Hits            ?      1558           
  Misses          ?         0           
  Partials        ?         0           
Impacted Files Coverage Δ
openapi_python_client/parser/openapi.py 100.00% <100.00%> (ø)
...penapi_python_client/parser/properties/__init__.py 100.00% <100.00%> (ø)
..._python_client/parser/properties/model_property.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 67db1a1...7cbeb7e. Read the comment docs.

@csymeonides-mf
Copy link
Contributor Author

@dbanty This is the other PR I mentioned :)

@csymeonides-mf
Copy link
Contributor Author

Btw I only had to move those golden record files so that I could import one of them for the e2e test (the directory was golden-record but import doesn't like hyphens so I changed it to golden_record) .

I didn't realise this but there might be another option if you prefer to keep the existing directory name.

@csymeonides-mf
Copy link
Contributor Author

@dbanty Now rebased

@csymeonides-mf
Copy link
Contributor Author

csymeonides-mf commented Apr 13, 2021

@dbanty Now rebased again

@dbanty
Copy link
Collaborator

dbanty commented Apr 25, 2021

Seems like the main reason for switching the names around was to import the generated code to do a more unit-test thing within the e2e test. The golden-record stuff is really intended to be a snapshot test, an easy way for me to see in reviews how the changes impact the generated code. This makes it easy to pick out accidental changes.

We probably want to have more specific tests of the generated code to make sure they do what's intended eventually, but I think we'd want to target those more (e.g. just generate one specific Jinja template with specific params and make sure it does what it's supposed to). I haven't decided on a strategy to achieve that yet.

So my recommendation here would be to drop out the unit test of the generated code and move the name back to golden-record so it's more obvious what changed.

@csymeonides-mf
Copy link
Contributor Author

I guess in my mind it was an end-to-end test of the library, but no worries, I'll change it to a unit test.

@dbanty
Copy link
Collaborator

dbanty commented Apr 25, 2021

Yeah I should probably change the name. It was intended to be integration tests as well but those got really hard to maintain and didn't add much value so it became just snapshot tests. We probably need some middle ground targeted integration tests that are separate from the unit tests of the generator itself but not as complicated as the full-on end-to-end generation snapshot.

@csymeonides-mf
Copy link
Contributor Author

I couldn't find a way to convert my "end-to-end" test to a unit test, but I suppose the fact that I've added those extra bits to openapi.json means we are asserting that the correct code is generated via the templates. Let me know if there's anything more I could be doing there.

@csymeonides-mf csymeonides-mf force-pushed the fix-multipart-file branch 2 times, most recently from cbe40af to b5b5833 Compare May 17, 2021 11:06
@csymeonides-mf
Copy link
Contributor Author

@dbanty Now rebased, could we get this into the next release?

@dbanty
Copy link
Collaborator

dbanty commented May 17, 2021

Yes, sorry! I keep starting to review this and getting pulled away 😅. I have a few questions that I'll try and put some comments on today so this can move forward.

@dbanty dbanty added this to the Next milestone May 17, 2021
@dbanty
Copy link
Collaborator

dbanty commented May 17, 2021

Great, so it seems like there are a few action items then, some of which may be handled in separate tasks:

  • Make sure to not include UNSET in any type of body. Right now that means using to_dict() or similar for both application/x-www-form-urlencoded and form/multipart. This is already being done with a combination of this PR and Fix(template): form data model serialization #430 .
  • Explicitly set the default content-type to text/plain and use the files input instead of the data input for multipart.
  • Support the encoding attribute (maybe just for application/json for now?) so that we know to jsonify certain parameters if specificed. For now it sounds like defaulting any complex objects (arrays, data classes) to json is a sane default since there's no clear way to represent them in plain text.
  • Avoid runtime type checking for the above unless necessary (e.g. Union). If we know the type and the encoding for that type we should avoid the overhead.

Some of those things may already be done by the time I comment this because some new commits just came in from @csymeonides-mf 😆 . Thanks for all the feedback and discussion around this!

@csymeonides-mf csymeonides-mf force-pushed the fix-multipart-file branch 2 times, most recently from bc3d0f3 to 08fd630 Compare May 17, 2021 21:16
@csymeonides-mf
Copy link
Contributor Author

Ok, I don't think I've done all the things on your list, but hopefully enough for now!

@csymeonides-mf csymeonides-mf marked this pull request as draft May 19, 2021 08:23
@csymeonides-mf
Copy link
Contributor Author

I've set this back to draft because when I tested it with a string field (not JSON) I realised that httpx doesn't handle them correctly - it converts them into files. Maybe I should be JSON-encoding strings as well? But that sounds wrong. Putting them in data as before will fix this, but again doesn't sound like the right solution.

@p1-ra if you have any ideas that would be much appreciated.

@csymeonides-mf
Copy link
Contributor Author

@dbanty this also made me realise that the e2e test I had written earlier would have caught this problem... do you have any advice on how we can re-introduce that test in a way that fits your approach? As I said earlier I couldn't find a way to unit-test this, because it involves running the code which has been generated from the jinja templates.

@csymeonides-mf csymeonides-mf force-pushed the fix-multipart-file branch 2 times, most recently from 5ab8f17 to 172921d Compare May 19, 2021 19:28
@csymeonides-mf csymeonides-mf marked this pull request as ready for review May 19, 2021 19:38
@csymeonides-mf
Copy link
Contributor Author

@dbanty All fixed now!

Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great work, thank you! Just a couple suggestions then I think we're good to go.

As far as being able to run the e2e-generated code for better testing, I have two ideas:

  1. Generate tests for the generated code. I have seen other generators include tests in the box before, so maybe that's something folks want anyway?
  2. Use update instead of generate when regenerating golden-record and manually maintain a tests directory in golden-record which tests the generated code. If we wanted to we could even go so far as spawning a local server to test against (this project actually used to do that using FastAPI when it first started).

In either case, we could then just run pytest in that directory to run those tests. Thoughts on either of those paths? This would be in a different, future PR, I just agree that we need some more thorough tests on the generated code to be more confident in changes going forward.

@dbanty dbanty modified the milestones: 0.9.2, 0.10.0 May 24, 2021
@csymeonides-mf
Copy link
Contributor Author

  1. Use update instead of generate when regenerating golden-record and manually maintain a tests directory in golden-record which tests the generated code. If we wanted to we could even go so far as spawning a local server to test against (this project actually used to do that using FastAPI when it first started).

This sounds great to me, though maybe we still want a separate test that runs generate to make sure that's covered as well?

@csymeonides-mf csymeonides-mf force-pushed the fix-multipart-file branch 3 times, most recently from 33bc36a to 9b301b2 Compare May 25, 2021 10:37
@csymeonides-mf csymeonides-mf requested a review from dbanty May 25, 2021 11:08
Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks so much for putting all this work into this!

@dbanty dbanty merged commit be48568 into openapi-generators:main Jun 2, 2021
dbanty pushed a commit that referenced this pull request Jun 2, 2021
…r complex types (#372). Thanks @csymeonides-mf!

* fix: Detect File fields correctly

* fix: Optional File field caused missing import error

* fix: Non-string multipart fields must be stringified

* chore: Reformat

* fix: Put non-file fields as tuples in the files dict

* refactor: Avoid runtime type checks

* fix: Declaring types is necessary

* fix: Remove dead code

* fix: Non-JSON non-file multipart props need to be text/plain

* refactor: Avoid breaking change

Co-authored-by: Constantinos Symeonides <[email protected]>
dbanty pushed a commit that referenced this pull request Jun 14, 2021
…r complex types (#372). Thanks @csymeonides-mf!

* fix: Detect File fields correctly

* fix: Optional File field caused missing import error

* fix: Non-string multipart fields must be stringified

* chore: Reformat

* fix: Put non-file fields as tuples in the files dict

* refactor: Avoid runtime type checks

* fix: Declaring types is necessary

* fix: Remove dead code

* fix: Non-JSON non-file multipart props need to be text/plain

* refactor: Avoid breaking change

Co-authored-by: Constantinos Symeonides <[email protected]>
@csymeonides-mf csymeonides-mf deleted the fix-multipart-file branch July 8, 2021 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants