Skip to content

Commit 88b3be1

Browse files
paulo-racadbanty
andauthored
Support multiple tags in each endpoint (openapi-generators#687)
Currently when an endpoint has multiple tags, the first tag is used and everything else is ignored. This PR modifies it so endpoints with multiple tags are added to each of the tags. Yes, this results in repeated code 😅, but works beautifully and functions can now be found anywhere we expect them to be. --------- Co-authored-by: Dylan Anthony <[email protected]> Co-authored-by: Dylan Anthony <[email protected]>
1 parent 7225f0e commit 88b3be1

File tree

14 files changed

+175
-120
lines changed

14 files changed

+175
-120
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
default: minor
3+
---
4+
5+
# Add `generate_all_tags` config option
6+
7+
You can now, optionally, generate **duplicate** endpoint functions/modules using _every_ tag for an endpoint,
8+
not just the first one, by setting `generate_all_tags: true` in your configuration file.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ literal_enums: true
108108

109109
This is especially useful if enum values, when transformed to their Python names, end up conflicting due to case sensitivity or special symbols.
110110

111+
### generate_all_tags
112+
113+
`openapi-python-client` generates module names within the `api` module based on the OpenAPI `tags` of each endpoint.
114+
By default, only the _first_ tag is generated. If you want to generate **duplicate** endpoint functions using _every_ tag
115+
listed, you can enable this option:
116+
117+
```yaml
118+
generate_all_tags: true
119+
```
120+
111121
### project_name_override and package_name_override
112122

113123
Used to change the name of generated client library project/package. If the project name is changed but an override for the package name

end_to_end_tests/__snapshots__/test_end_to_end.ambr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
# serializer version: 1
2+
# name: test_documents_with_errors[bad-status-code]
3+
'''
4+
Generating /test-documents-with-errors
5+
Warning(s) encountered while generating. Client was generated, but some pieces may be missing
6+
7+
WARNING parsing GET / within default.
8+
9+
Invalid response status code abcdef (not a valid HTTP status code), response will be omitted from generated client
10+
11+
12+
If you believe this was a mistake or this tool is missing a feature you need, please open an issue at https://github.com/openapi-generators/openapi-python-client/issues/new/choose
13+
14+
'''
15+
# ---
216
# name: test_documents_with_errors[circular-body-ref]
317
'''
418
Generating /test-documents-with-errors

end_to_end_tests/baseline_openapi_3.0.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,9 +1149,7 @@
11491149
},
11501150
"/tag_with_number": {
11511151
"get": {
1152-
"tags": [
1153-
"1"
1154-
],
1152+
"tags": ["1", "2"],
11551153
"responses": {
11561154
"200": {
11571155
"description": "Success"

end_to_end_tests/baseline_openapi_3.1.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,9 +1141,7 @@ info:
11411141
},
11421142
"/tag_with_number": {
11431143
"get": {
1144-
"tags": [
1145-
"1"
1146-
],
1144+
"tags": ["1", "2"],
11471145
"responses": {
11481146
"200": {
11491147
"description": "Success"

end_to_end_tests/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ class_overrides:
1111
field_prefix: attr_
1212
content_type_overrides:
1313
openapi/python/client: application/json
14+
generate_all_tags: true

end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .parameters import ParametersEndpoints
1212
from .responses import ResponsesEndpoints
1313
from .tag1 import Tag1Endpoints
14+
from .tag2 import Tag2Endpoints
1415
from .tests import TestsEndpoints
1516
from .true_ import True_Endpoints
1617

@@ -48,6 +49,10 @@ def parameters(cls) -> type[ParametersEndpoints]:
4849
def tag1(cls) -> type[Tag1Endpoints]:
4950
return Tag1Endpoints
5051

52+
@classmethod
53+
def tag2(cls) -> type[Tag2Endpoints]:
54+
return Tag2Endpoints
55+
5156
@classmethod
5257
def location(cls) -> type[LocationEndpoints]:
5358
return LocationEndpoints
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Contains methods for accessing the API Endpoints"""
2+
3+
import types
4+
5+
from . import get_tag_with_number
6+
7+
8+
class Tag2Endpoints:
9+
@classmethod
10+
def get_tag_with_number(cls) -> types.ModuleType:
11+
return get_tag_with_number
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: "3.1.0"
2+
info:
3+
title: "There's something wrong with me"
4+
version: "0.1.0"
5+
paths:
6+
"/":
7+
get:
8+
responses:
9+
"abcdef":
10+
description: "Successful Response"
11+
content:
12+
"application/json":
13+
schema:
14+
const: "Why have a fixed response? I dunno"

end_to_end_tests/golden-record/my_test_api_client/api/tag2/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)