Skip to content

Commit 3de1de1

Browse files
authored
Merge pull request #190 from fingerprintjs/chore/INTER-2037-oneof-union-support
chore: add support for non-discriminator, oneOf query parameters
2 parents 03ed58b + cf5c361 commit 3de1de1

4 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎template/api.mustache‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ from typing_extensions import Annotated
88
{{#imports}}
99
{{import}}
1010
{{/imports}}
11+
{{#operations.operation}}
12+
{{! Workaround for missing imports for parameters that use oneOf }}
13+
{{#allParams}}
14+
{{^hasDiscriminatorWithNonEmptyMapping}}
15+
{{#composedSchemas.oneOf}}
16+
{{#-first}}
17+
from {{packageName}}.models.{{#lambda.snakecase}}{{schema.complexType}}{{/lambda.snakecase}} import {{schema.complexType}}
18+
from datetime import datetime, date, timezone
19+
{{/-first}}
20+
{{/composedSchemas.oneOf}}
21+
{{/hasDiscriminatorWithNonEmptyMapping}}
22+
{{/allParams}}
23+
{{/operations.operation}}
1124
from {{packageName}}.api_client import ApiClient, RequestSerialized
1225
from {{packageName}}.api_response import ApiResponse
1326
from {{packageName}}.configuration import Configuration
@@ -117,6 +130,7 @@ class {{classname}}:
117130
{{#queryParams}}
118131
# process the query parameters
119132
if {{paramName}} is not None:
133+
{{^schema.composedSchemas.oneOf}}
120134
{{#isDateTime}}
121135
if isinstance({{paramName}}, datetime):
122136
_query_params.append(
@@ -146,6 +160,27 @@ class {{classname}}:
146160
{{^isDateTime}}{{^isDate}}
147161
_query_params.append(('{{baseName}}', {{paramName}}{{#isEnumRef}}.value{{/isEnumRef}}))
148162
{{/isDate}}{{/isDateTime}}
163+
{{/schema.composedSchemas.oneOf}}
164+
{{#schema.composedSchemas.oneOf}}
165+
{{#-first}}
166+
if isinstance({{paramName}}, datetime):
167+
_query_params.append(
168+
(
169+
'{{schema.baseName}}',
170+
{{paramName}}.isoformat(timespec='microseconds')
171+
)
172+
)
173+
elif isinstance({{paramName}}, date):
174+
_query_params.append(
175+
(
176+
'{{schema.baseName}}',
177+
{{paramName}}.isoformat()
178+
)
179+
)
180+
else:
181+
_query_params.append(('{{schema.baseName}}', {{paramName}}))
182+
{{/-first}}
183+
{{/schema.composedSchemas.oneOf}}
149184
{{/queryParams}}
150185

151186
{{#headerParams}}

‎template/model_doc.mustache‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ Name | Type | Description | Notes
1111
{{#vars}}**{{name}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}[**{{dataType}}**]({{complexType}}.md){{/isPrimitiveType}} | {{{description}}} | {{^required}}[optional] {{/required}}{{#isReadOnly}}[readonly] {{/isReadOnly}}{{#defaultValue}}[default to {{{.}}}]{{/defaultValue}}
1212
{{/vars}}
1313
{{/emptyVars}}
14+
{{#emptyVars}}
15+
{{^model.discriminator}}
16+
{{#composedSchemas.oneOf}}
17+
{{#-first}}
18+
## Type alias
19+
20+
`Union[{{#oneOf}}{{^-last}}{{.}},{{/-last}}{{#-last}}{{.}}{{/-last}}{{/oneOf}}]`
21+
{{/-first}}
22+
{{/composedSchemas.oneOf}}
23+
{{/model.discriminator}}
24+
{{/emptyVars}}
1425
{{/isEnum}}
1526
{{#isEnum}}
1627
## Enum

‎template/model_oneof.mustache‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{#hasDiscriminatorWithNonEmptyMapping}}
12
from __future__ import annotations
23
import json
34
import pprint
@@ -206,3 +207,13 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
206207
# TODO: Rewrite to not use raise_errors
207208
{{classname}}.model_rebuild(raise_errors=False)
208209
{{/vendorExtensions.x-py-postponed-model-imports.size}}
210+
{{/hasDiscriminatorWithNonEmptyMapping}}
211+
{{^hasDiscriminatorWithNonEmptyMapping}}
212+
from typing import Union
213+
{{#composedSchemas.oneOf}}
214+
{{#isDateTime}}
215+
from pydantic import AwareDatetime
216+
{{/isDateTime}}
217+
{{/composedSchemas.oneOf}}
218+
{{classname}} = Union[{{#composedSchemas.oneOf}}{{#isDateTime}}AwareDatetime{{/isDateTime}}{{^isDateTime}}{{dataType}}{{/isDateTime}}{{^-last}},{{/-last}}{{/composedSchemas.oneOf}}]
219+
{{/hasDiscriminatorWithNonEmptyMapping}}

‎template/partial_api_args.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(
22
self,
33
{{#allParams}}
4-
{{paramName}}: {{{vendorExtensions.x-py-typing}}}{{^required}} = None{{/required}},
4+
{{paramName}}: {{^schema.composedSchemas}}{{{vendorExtensions.x-py-typing}}}{{/schema.composedSchemas}}{{#schema.composedSchemas}}Annotated[{{^required}}Optional[{{/required}}{{schema.complexType}}{{^required}}]{{/required}},Field(description="{{{description}}}",)]{{/schema.composedSchemas}}{{^required}} = None{{/required}},
55
{{/allParams}}
66
_request_timeout: Union[
77
None,

0 commit comments

Comments
 (0)