Skip to content

Commit 3253448

Browse files
authored
Add OpenAPI 3.1.0 support (#856)
1 parent fe76d59 commit 3253448

File tree

142 files changed

+13420
-2184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+13420
-2184
lines changed

.changeset/openapi_31_support.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
default: minor
3+
---
4+
5+
# OpenAPI 3.1 support
6+
7+
The generator will now attempt to generate code for OpenAPI documents with versions 3.1.x (previously, it would exit immediately on seeing a version other than 3.0.x). The following specific OpenAPI 3.1 features are now supported:
8+
9+
- `null` as a type
10+
- Arrays of types (e.g., `type: [string, null]`)
11+
- `const` (defines `Literal` types)
12+
13+
The generator does not currently validate that the OpenAPI document is valid for a specific version of OpenAPI, so it may be possible to generate code for documents that include both removed 3.0 syntax (e.g., `nullable`) and new 3.1 syntax (e.g., `null` as a type).
14+
15+
Thanks to everyone who helped make this possible with discussions and testing, including:
16+
17+
- @frco9
18+
- @vogre
19+
- @naddeoa
20+
- @staticdev
21+
- @philsturgeon
22+
- @johnthagen
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
default: major
3+
---
4+
5+
# Removed query parameter nullable/required special case
6+
7+
In previous versions, setting _either_ `nullable: true` or `required: false` on a query parameter would act like both were set, resulting in a type signature like `Union[None, Unset, YourType]`. This special case has been removed, query parameters will now act like all other types of parameters.

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ htmlcov/
2727

2828
# Generated end to end test data
2929
my-test-api-client/
30-
custom-e2e/
30+
custom-e2e/
31+
3-1-features-client

CONTRIBUTING.md

+29-24
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ A bug is one of:
1212
2. The generated code is invalid or incorrect
1313
3. An error message is unclear or incorrect
1414
4. Something which used to work no longer works, except:
15-
1. Intentional breaking changes, which are documented in the [changelog](https://github.com/openapi-generators/openapi-python-client/blob/main/CHANGELOG.md)
16-
2. Breaking changes to unstable features, like custom templates
15+
1. Intentional breaking changes, which are documented in the [changelog](https://github.com/openapi-generators/openapi-python-client/blob/main/CHANGELOG.md)
16+
2. Breaking changes to unstable features, like custom templates
1717

1818
If your issue does not fall under one of the above, it is not a bug; check out "[Requesting a feature](#requesting-a-feature).
1919

@@ -27,14 +27,14 @@ A feature is usually:
2727

2828
1. An improvement to the way the generated code works
2929
2. A feature of the generator itself which makes its use easier (e.g., a new config option)
30-
3. **Support for part of the OpenAPI spec**; this generate _does not yet_ support every OpenAPI feature, these missing features **are not bugs**.
30+
3. **Support for part of the OpenAPI spec**; this generator _does not yet_ support every OpenAPI feature, these missing features **are not bugs**.
3131

3232
To request a feature:
3333

3434
1. Search through [discussions](https://github.com/openapi-generators/openapi-python-client/discussions/categories/feature-request) to see if the feature you want has already been requested. If it has:
35-
1. Upvote it with the little arrow on the original post. This enables code contributors to prioritize the most-demanded features.
36-
2. Optionally leave a comment describing why _you_ want the feature, if no existing thread already covers your use-case
37-
3. If a relevant discussion does not already exist, create a new one. If you are not requesting support for part of the OpenAPI spec, **you must** describe _why_ you want the feature. What real-world use-case does it improve? For example, "raise exceptions for invalid responses" might have a description of "it's not worth the effort to check every error case by hand for the one-off scripts I'm writing".
35+
1. Upvote it with the little arrow on the original post. This enables code contributors to prioritize the most-demanded features.
36+
2. Optionally leave a comment describing why _you_ want the feature, if no existing thread already covers your use-case
37+
2. If a relevant discussion does not already exist, create a new one. If you are not requesting support for part of the OpenAPI spec, **you must** describe _why_ you want the feature. What real-world use-case does it improve? For example, "raise exceptions for invalid responses" might have a description of "it's not worth the effort to check every error case by hand for the one-off scripts I'm writing".
3838

3939
## Contributing Code
4040

@@ -45,36 +45,41 @@ To request a feature:
4545
3. Use `poetry install` in the project directory to create a virtual environment with the relevant dependencies.
4646
4. Enter a `poetry shell` to make running commands easier.
4747

48-
### Writing Code
48+
### Writing tests
4949

50-
1. Write some code and make sure it's covered by unit tests. All unit tests are in the `tests` directory and the file structure should mirror the structure of the source code in the `openapi_python_client` directory.
50+
All changes must be tested, I recommend writing the test first, then writing the code to make it pass. 100% code coverage is enforced in CI, a check will fail in GitHub if your code does not have 100% coverage. An HTML report will be added to the test artifacts in this case to help you locate missed lines.
5151

52-
#### Run Checks and Tests
52+
If you think that some of the added code is not testable (or testing it would add little value), mention that in your PR and we can discuss it.
5353

54-
2. When in a Poetry shell (`poetry shell`) run `task check` in order to run most of the same checks CI runs. This will auto-reformat the code, check type annotations, run unit tests, check code coverage, and lint the code.
54+
1. If you're adding support for a new OpenAPI feature or covering a new edge case, add an [end-to-end test](#end-to-end-tests)
55+
2. If you're modifying the way an existing feature works, make sure an existing test generates the _old_ code in `end_to_end_tests/golden-record`. You'll use this to check for the new code once your changes are complete.
56+
3. If you're improving an error or adding a new error, add a [unit test](#unit-tests)
5557

56-
#### Rework end-to-end tests
58+
#### End-to-end tests
5759

58-
3. If you're writing a new feature, try to add it to the end-to-end test.
59-
1. If adding support for a new OpenAPI feature, add it somewhere in `end_to_end_tests/openapi.json`
60-
2. Regenerate the "golden records" with `task regen`. This client is generated from the OpenAPI document used for end-to-end testing.
61-
3. Check the changes to `end_to_end_tests/golden-record` to confirm only what you intended to change did change and that the changes look correct.
62-
4. **If you added a test above OR modified the templates**: Run the end-to-end tests with `task e2e`. This will generate clients against `end_to_end_tests/openapi.json` and compare them with the golden record. The tests will fail if **anything is different**. The end-to-end tests are not included in `task check` as they take longer to run and don't provide very useful feedback in the event of failure. If an e2e test does fail, the easiest way to check what's wrong is to run `task regen` and check the diffs. You can also use `task re` which will run `regen` and `e2e` in that order.
60+
This project aims to have all "happy paths" (types of code which _can_ be generated) covered by end to end tests (snapshot tests). In order to check code changes against the previous set of snapshots (called a "golden record" here), you can run `poetry run task e2e`. To regenerate the snapshots, run `poetry run task regen`.
6361

62+
There are 4 types of snapshots generated right now, you may have to update only some or all of these depending on the changes you're making. Within the `end_to_end_tets` directory:
6463

65-
### Creating a Pull Request
64+
1. `baseline_openapi_3.0.json` creates `golden-record` for testing OpenAPI 3.0 features
65+
2. `baseline_openapi_3.1.yaml` is checked against `golden-record` for testing OpenAPI 3.1 features (and ensuring consistency with 3.0)
66+
3. `test_custom_templates` are used with `baseline_openapi_3.0.json` to generate `custom-templates-golden-record` for testing custom templates
67+
4. `3.1_specific.openapi.yaml` is used to generate `test-3-1-golden-record` and test 3.1-specific features (things which do not have a 3.0 equivalent)
68+
69+
#### Unit tests
6670

67-
Once you've written the code and run the checks, the next step is to create a pull request against the `main` branch of this repository. This repository uses [conventional commits] squashed on each PR, then uses [Knope] to auto-generate CHANGELOG.md entries for release. So the title of your PR should be in the format of a conventional commit written in plain english as it will end up in the CHANGELOG. Some example PR titles:
71+
> **NOTE**: Several older-style unit tests using mocks exist in this project. These should be phased out rather than updated, as the tests are brittle and difficult to maintain. Only error cases should be tests with unit tests going forward.
6872
69-
- feat: Support for `allOf` in OpenAPI documents (closes #123).
70-
- refactor!: Removed support for Python 3.5
71-
- fix: Data can now be passed to multipart bodies along with files.
73+
In some cases, we need to test things which cannot be generated—like validating that errors are caught and handled correctly. These should be tested via unit tests in the `tests` directory, using the `pytest` framework.
74+
75+
### Creating a Pull Request
7276

73-
Once your PR is created, a series of automated checks should run. If any of them fail, try your best to fix them.
77+
Once you've written the tests and code and run the checks, the next step is to create a pull request against the `main` branch of this repository. This repository uses [Knope] to auto-generate release notes and version numbers. This can either be done by setting the title of the PR to a [conventional commit] (for simple changes) or by adding [changesets]. If the changes are not documented yet, a check will fail on GitHub. The details of this check will have suggestions for documenting the change (including an example change file for changesets).
7478

7579
### Wait for Review
7680

7781
As soon as possible, your PR will be reviewed. If there are any changes requested there will likely be a bit of back and forth. Once this process is done, your changes will be merged into main and included in the next release. If you need your changes available on PyPI by a certain time, please mention it in the PR, and we'll do our best to accommodate.
7882

79-
[Conventional Commits]: https://www.conventionalcommits.org/en/v1.0.0/
80-
[Knope]: https://knope-dev.github.io/knope/
83+
[Knope]: https://knope.tech
84+
[changesets]: https://knope.tech/reference/concepts/changeset/
85+
[Conventional Commits]: https://knope.tech/reference/concepts/conventional-commits/

README.md

+9-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# openapi-python-client
99

10-
Generate modern Python clients from OpenAPI 3.0 documents.
10+
Generate modern Python clients from OpenAPI 3.0 and 3.1 documents.
1111

1212
_This generator does not support OpenAPI 2.x FKA Swagger. If you need to use an older document, try upgrading it to
1313
version 3 first with one of many available converters._
@@ -69,25 +69,16 @@ _Be forewarned, this is a beta-level feature in the sense that the API exposed i
6969
## What You Get
7070

7171
1. A `pyproject.toml` file with some basic metadata intended to be used with [Poetry].
72-
1. A `README.md` you'll most definitely need to update with your project's details
73-
1. A Python module named just like the auto-generated project name (e.g. "my_api_client") which contains:
72+
2. A `README.md` you'll most definitely need to update with your project's details
73+
3. A Python module named just like the auto-generated project name (e.g. "my_api_client") which contains:
7474
1. A `client` module which will have both a `Client` class and an `AuthenticatedClient` class. You'll need these
7575
for calling the functions in the `api` module.
76-
1. An `api` module which will contain one module for each tag in your OpenAPI spec, as well as a `default` module
76+
2. An `api` module which will contain one module for each tag in your OpenAPI spec, as well as a `default` module
7777
for endpoints without a tag. Each of these modules in turn contains one function for calling each endpoint.
78-
1. A `models` module which has all the classes defined by the various schemas in your OpenAPI spec
78+
3. A `models` module which has all the classes defined by the various schemas in your OpenAPI spec
7979

80-
For a full example you can look at the `end_to_end_tests` directory which has an `openapi.json` file.
81-
"golden-record" in that same directory is the generated client from that OpenAPI document.
82-
83-
## OpenAPI features supported
84-
85-
1. All HTTP Methods
86-
1. JSON and form bodies, path and query parameters
87-
1. File uploads with multipart/form-data bodies
88-
1. float, string, int, date, datetime, string enums, and custom schemas or lists containing any of those
89-
1. html/text or application/json responses containing any of the previous types
90-
1. Bearer token security
80+
For a full example you can look at the `end_to_end_tests` directory which has `baseline_openapi_3.0.json` and `baseline_openapi_3.1.yaml` files.
81+
The "golden-record" in that same directory is the generated client from either of those OpenAPI documents.
9182

9283
## Configuration
9384

@@ -98,7 +89,7 @@ The following parameters are supported:
9889

9990
Used to change the name of generated model classes. This param should be a mapping of existing class name
10091
(usually a key in the "schemas" section of your OpenAPI document) to class_name and module_name. As an example, if the
101-
name of the a model in OpenAPI (and therefore the generated class name) was something like "\_PrivateInternalLongName"
92+
name of a model in OpenAPI (and therefore the generated class name) was something like "_PrivateInternalLongName"
10293
and you want the generated client's model to be called "ShortName" in a module called "short_name" you could do this:
10394

10495
Example:
@@ -110,7 +101,7 @@ class_overrides:
110101
module_name: short_name
111102
```
112103
113-
The easiest way to find what needs to be overridden is probably to generate your client and go look at everything in the models folder.
104+
The easiest way to find what needs to be overridden is probably to generate your client and go look at everything in the `models` folder.
114105

115106
### project_name_override and package_name_override
116107

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
openapi: "3.1.0"
2+
info:
3+
title: "Test 3.1 Features"
4+
description: "Test new OpenAPI 3.1 features"
5+
version: "0.1.0"
6+
paths:
7+
"/const/{path}":
8+
post:
9+
tags: [ "const" ]
10+
parameters:
11+
- in: "path"
12+
required: true
13+
schema:
14+
const: "this goes in the path"
15+
name: "path"
16+
- in: "query"
17+
required: true
18+
schema:
19+
const: "this always goes in the query"
20+
name: "required query"
21+
- in: "query"
22+
schema:
23+
const: "this sometimes goes in the query"
24+
name: "optional query"
25+
requestBody:
26+
required: true
27+
content:
28+
"application/json":
29+
schema:
30+
type: object
31+
properties:
32+
required:
33+
const: "this always goes in the body"
34+
optional:
35+
const: "this sometimes goes in the body"
36+
nullable:
37+
oneOf:
38+
- type: "null"
39+
- const: "this or null goes in the body"
40+
required:
41+
- required
42+
- nullable
43+
responses:
44+
"200":
45+
description: "Successful Response"
46+
content:
47+
"application/json":
48+
schema:
49+
const: "Why have a fixed response? I dunno"

end_to_end_tests/openapi.json end_to_end_tests/baseline_openapi_3.0.json

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"openapi": "3.0.2",
2+
"openapi": "3.0.3",
33
"info": {
44
"title": "My Test API",
55
"description": "An API for testing openapi-python-client",
@@ -449,10 +449,10 @@
449449
}
450450
}
451451
},
452-
"/tests/defaults": {
452+
"/defaults": {
453453
"post": {
454454
"tags": [
455-
"tests"
455+
"defaults"
456456
],
457457
"summary": "Defaults",
458458
"operationId": "defaults_tests_defaults_post",
@@ -467,6 +467,16 @@
467467
"name": "string_prop",
468468
"in": "query"
469469
},
470+
{
471+
"required": true,
472+
"schema": {
473+
"title": "String with num default",
474+
"type": "string",
475+
"default": 1
476+
},
477+
"name": "string with num",
478+
"in": "query"
479+
},
470480
{
471481
"required": true,
472482
"schema": {
@@ -2425,7 +2435,8 @@
24252435
"in": "header",
24262436
"required": false,
24272437
"schema": {
2428-
"type": "string"
2438+
"type": "string",
2439+
"nullable": true
24292440
}
24302441
},
24312442
"cookie-param": {

0 commit comments

Comments
 (0)