Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 8318ab9

Browse files
authored
Adds 3.1.0 const feature (#210)
* Adds generation of const enum class and storage of const values in schema * Writes const info in schema class when a type is defined * Adds const validator and disableable keyword * Adds const to the generator docs * Fixes validation writing for str schema * Handles constInfo in boolean/string/integer validate overloads * Fixes validate else conditions * Another fix to the validate template for enums * Fixes literal casting using constInfo in validate * Stops using type in validate method, uses types instead * Refactors enumInfo for validate return type to be simpler * Fixes validate templete for constInfo * Sequence fixes in schema.py and schemas.py * Adds and uses SequenceNotStr * Fixes typo * Adds tests of const * Samples regen * Samples regenerated
1 parent 8ff6b0e commit 8318ab9

File tree

90 files changed

+947
-261
lines changed

Some content is hidden

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

90 files changed

+947
-261
lines changed

docs/generators/java.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
316316
|AdditionalProperties|✗|OAS2,OAS3
317317
|AllOf|✗|OAS2,OAS3
318318
|AnyOf|✗|OAS3
319+
|Const|✗|OAS3
319320
|Contains|✗|OAS3
320321
|Default|✗|OAS2,OAS3
321322
|Discriminator|✓|OAS2,OAS3

docs/generators/jaxrs-jersey.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
299299
|AdditionalProperties|✗|OAS2,OAS3
300300
|AllOf|✗|OAS2,OAS3
301301
|AnyOf|✗|OAS3
302+
|Const|✗|OAS3
302303
|Contains|✗|OAS3
303304
|Default|✗|OAS2,OAS3
304305
|Discriminator|✓|OAS2,OAS3

docs/generators/jmeter.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
158158
|AdditionalProperties|✗|OAS2,OAS3
159159
|AllOf|✗|OAS2,OAS3
160160
|AnyOf|✗|OAS3
161+
|Const|✗|OAS3
161162
|Contains|✗|OAS3
162163
|Default|✗|OAS2,OAS3
163164
|Discriminator|✓|OAS2,OAS3

docs/generators/kotlin.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
268268
|AdditionalProperties|✗|OAS2,OAS3
269269
|AllOf|✗|OAS2,OAS3
270270
|AnyOf|✗|OAS3
271+
|Const|✗|OAS3
271272
|Contains|✗|OAS3
272273
|Default|✗|OAS2,OAS3
273274
|Discriminator|✓|OAS2,OAS3

docs/generators/python.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
227227
|AdditionalProperties|✓|OAS2,OAS3
228228
|AllOf|✓|OAS2,OAS3
229229
|AnyOf|✓|OAS3
230+
|Const|✓|OAS3
230231
|Contains|✓|OAS3
231232
|Default|✓|OAS2,OAS3
232233
|Discriminator|✓|OAS2,OAS3

samples/client/3_0_3_unit_test/python/src/unit_test_api/components/schema/enum_with_escaped_characters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def FOO_CARRIAGE_RETURN_CR_BAR(cls) -> typing.Literal["foo\rbar"]:
2525

2626
@dataclasses.dataclass(frozen=True)
2727
class EnumWithEscapedCharacters(
28-
schemas.Schema[schemas.immutabledict, str]
28+
schemas.Schema
2929
):
3030
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
3131
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator

samples/client/3_0_3_unit_test/python/src/unit_test_api/components/schema/enums_in_properties.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def FOO(cls) -> typing.Literal["foo"]:
2121

2222
@dataclasses.dataclass(frozen=True)
2323
class Foo(
24-
schemas.Schema[schemas.immutabledict, str]
24+
schemas.Schema
2525
):
2626
types: typing.FrozenSet[typing.Type] = frozenset({
2727
str,
@@ -75,7 +75,7 @@ def BAR(cls) -> typing.Literal["bar"]:
7575

7676
@dataclasses.dataclass(frozen=True)
7777
class Bar(
78-
schemas.Schema[schemas.immutabledict, str]
78+
schemas.Schema
7979
):
8080
types: typing.FrozenSet[typing.Type] = frozenset({
8181
str,

samples/client/3_0_3_unit_test/python/src/unit_test_api/components/schema/nul_characters_in_strings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def HELLO_NULL_THERE(cls) -> typing.Literal["hello\x00there"]:
2121

2222
@dataclasses.dataclass(frozen=True)
2323
class NulCharactersInStrings(
24-
schemas.Schema[schemas.immutabledict, str]
24+
schemas.Schema
2525
):
2626
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
2727
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator

samples/client/3_0_3_unit_test/python/src/unit_test_api/configurations/schema_configuration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'additional_properties': 'additionalProperties',
1717
'all_of': 'allOf',
1818
'any_of': 'anyOf',
19+
'const_value_to_name': 'const',
1920
'contains': 'contains',
2021
'discriminator': 'discriminator',
2122
# default omitted because it has no validation impact

samples/client/3_0_3_unit_test/python/src/unit_test_api/schemas/schema.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,33 @@
1414

1515
from . import validation
1616

17+
_T_co = typing.TypeVar("_T_co", covariant=True)
18+
19+
20+
class SequenceNotStr(typing.Protocol[_T_co]):
21+
"""
22+
if a Protocol would define the interface of Sequence, this protocol
23+
would NOT allow str/bytes as their __contains__ methods are incompatible with the definition in Sequence
24+
methods from: https://docs.python.org/3/library/collections.abc.html#collections.abc.Collection
25+
"""
26+
def __contains__(self, value: object, /) -> bool:
27+
raise NotImplementedError
28+
29+
def __getitem__(self, index, /):
30+
raise NotImplementedError
31+
32+
def __len__(self) -> int:
33+
raise NotImplementedError
34+
35+
def __iter__(self) -> typing.Iterator[_T_co]:
36+
raise NotImplementedError
37+
38+
def __reversed__(self, /) -> typing.Iterator[_T_co]:
39+
raise NotImplementedError
40+
1741
none_type_ = type(None)
1842
T = typing.TypeVar('T', bound=typing.Mapping)
19-
U = typing.TypeVar('U', bound=typing.Sequence)
43+
U = typing.TypeVar('U', bound=SequenceNotStr)
2044
W = typing.TypeVar('W')
2145

2246

@@ -439,15 +463,15 @@ def validate_base(
439463
@classmethod
440464
def validate_base(
441465
cls,
442-
arg: typing.Union[datetime.date, datetime.datetime, uuid.UUID],
466+
arg: typing.Union[str, datetime.date, datetime.datetime, uuid.UUID],
443467
configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
444468
) -> str: ...
445469

446470
@typing.overload
447471
@classmethod
448472
def validate_base(
449473
cls,
450-
arg: typing.Sequence[INPUT_TYPES_ALL], # also covers str, tuple, list, bytes
474+
arg: SequenceNotStr[INPUT_TYPES_ALL],
451475
configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
452476
) -> U: ...
453477

@@ -486,6 +510,14 @@ def validate_base(
486510
configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
487511
) -> FileIO: ...
488512

513+
@typing.overload
514+
@classmethod
515+
def validate_base(
516+
cls,
517+
arg: bytes,
518+
configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
519+
) -> bytes: ...
520+
489521
@classmethod
490522
def validate_base(
491523
cls,
@@ -614,15 +646,15 @@ def validate(
614646
@classmethod
615647
def validate(
616648
cls,
617-
arg: typing.Union[datetime.date, datetime.datetime, uuid.UUID],
649+
arg: typing.Union[str, datetime.date, datetime.datetime, uuid.UUID],
618650
configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
619651
) -> str: ...
620652

621653
@typing.overload
622654
@classmethod
623655
def validate(
624656
cls,
625-
arg: typing.Sequence[INPUT_TYPES_ALL], # also covers str, tuple, list, bytes
657+
arg: SequenceNotStr[INPUT_TYPES_ALL],
626658
configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
627659
) -> U: ...
628660

@@ -653,6 +685,14 @@ def validate(
653685
configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
654686
) -> FileIO: ...
655687

688+
@typing.overload
689+
@classmethod
690+
def validate(
691+
cls,
692+
arg: bytes,
693+
configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
694+
) -> bytes: ...
695+
656696
@classmethod
657697
def validate(
658698
cls,
@@ -668,20 +708,6 @@ class UnsetAnyTypeSchema(AnyTypeSchema[T, U]):
668708
# Used when additionalProperties/items was not explicitly defined and a defining schema is needed
669709
pass
670710

671-
INPUT_TYPES_NOT_STR_BYTES_FILE = typing.Union[
672-
dict,
673-
validation.immutabledict,
674-
list,
675-
tuple,
676-
float,
677-
int,
678-
datetime.date,
679-
datetime.datetime,
680-
uuid.UUID,
681-
bool,
682-
None,
683-
]
684-
685711
INPUT_TYPES_ALL = typing.Union[
686712
dict,
687713
validation.immutabledict,

0 commit comments

Comments
 (0)