-
-
Notifications
You must be signed in to change notification settings - Fork 228
Add OpenAPI 3.1.0 support #856
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
Conversation
# Conflicts: # openapi_python_client/parser/properties/__init__.py # openapi_python_client/parser/properties/model_property.py # tests/test_parser/test_openapi.py
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #856 +/- ##
===========================================
- Coverage 100.00% 98.58% -1.42%
===========================================
Files 49 61 +12
Lines 1940 2330 +390
===========================================
+ Hits 1940 2297 +357
- Misses 0 33 +33 ☔ View full report in Codecov by Sentry. |
Hello, The error is :
In fastAPI I declare the following Header in each endpoints (using a Depends) : def get_current_user(
x_act_as: Union[str, None] = Header(
None,
alias="XX-Act-As",
description=(
"This header allows a restricted list of admin user to impersonate "
"a user."
),
),
(...)
) Which output the following Openapi.yaml : (...)
get:
operationId: get_user
parameters:
- description: Id of the user to fetch
in: path
name: userId
required: true
schema:
description: Id of the user to fetch
format: uuid
type: string
- description: This header allows a restricted list of admin user to impersonate
a user.
in: header
name: XX-Act-As
required: false
schema:
description: This header allows a restricted list of admin user to impersonate
a user.
oneOf:
- type: string
- type: 'null'
(...) |
Thanks for reporting @frco9, this is exactly the kind of feedback I'm hoping to get before releasing this! I believe I fixed it in the latest commit |
Indeed it's working better @dbanty. Everything else seems ok on our side. Will let you know if we have other bugs |
Well we found another issue with We had in our 3.0 openapi a field used as discriminator with type a Literal from an Enum: class DatasetDB(Dataset):
dataset_type_code: Literal[DatasetTypeCode.TABLE] = Field(
DatasetTypeCode.TABLE, description="Dataset's type. Set to Table"
)
(...) The diff openapi is : (...)
datasetTypeCode:
default: table
description: Dataset's type. Set to Table
- enum:
- - table
- type: string
+ const: table
(...) Which is changed to a const in the 3.1 version While the changes for the generated client are : - dataset_type_code: Union[Unset, DatasetDBDatasetTypeCode] = DatasetDBDatasetTypeCode.TABLE
+ dataset_type_code: Union[Unset, Any] = UNSET I think proper output for client should be : dataset_type_code: Union[Unset, Literal["table"]] = "table" Thanks ! |
* chore(deps): update dependency knope to v0.13.1 (#888) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): lock file maintenance (#889) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency knope to v0.13.2 (#890) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): lock file maintenance (#891) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): lock file maintenance (#893) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * WIP support of `const` keyword * feat: Basic `const` support --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Dylan Anthony <[email protected]>
@frco9 I added the most basic form of |
# Conflicts: # openapi_python_client/parser/openapi.py
This branch seems to work well with all OpenAPI 3.1.0 schemas that we've thrown at it 💯 |
I tested the branch locally on our schema and it does look like it works, but I noticed that its used Is this a conscious choice? This milestone might be a good time to switch over to dataclasses. An alternative solution would be to make sure the name of the variable is the same name as the kwarg. Pyright kind of gets the idea if those two things line up, but the |
@naddeoa I don't remember why attrs was originally picked. There has been a bit of discussion around this (for example #574), but it is pretty low on my priority list to change that. The amount of work it would take to validate the idea is probably better spent adding support for more OpenAPI features (like #801, which is probably easier to do with attrs than dataclass). |
@dbanty Do you know why the variable names are prefixed with Pyright would actually work out with attrs (from my testing locally) if the names just didn't include the leading underscore on the classes that people interface with directly. |
The properties of the class are prefixed to indicate that they are private and you shouldn't access them directly (because there are methods for that which do extra stuff). Attrs correctly names the parameters in the constructor without that prefix because parameter names are not private. For the client classes in particular, it might be better to use traditional Python classes instead of attrs classes. But again, not the highest priority. I've never had luck with pyright, I always end up back with mypy when I try other type checkers 😅 |
I bring up pyright because the thing that drove me to this project was the goal of generating a "modern" client. Pyright/pylance seem to have all of the momentum and, from experience, they really do a much better job than mypy at so many issues, from generics to inference without type stubs, its been a real joy. I highly recommend. With the current state of the attr var names though, the auto generated stubs would generate errors for anyone using the "strict" pyright setting, which isn't a small number of people. Do you think naming them without underscores, and sacrificing the thin veil of privacy that isn't really enforced anyway in Python, is a good trade off to support the strict pyright users? I lean towards yes, in the aim of generating a modern client. I can see some people disagreeing of course, but having the strictest type checkers be ok with the generated code seems like a tangible goal with lots of benefits. Of course, its still light years better than the default openapi generator for Python, which I would honestly rather write a client from scratch than actually use nowadays. |
TODO:
Put back the special-case "Unset is None" code for query params(decided to remove special case instead)CONTRIBUTING.md
with new instructions (now that there are multiple e2e tests)