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

Commit 78425ba

Browse files
committed
v3 improves enums (#188)
* Adds int and str enum literal outputs * Adds enum literal outputs when possible
1 parent 08385d6 commit 78425ba

File tree

47 files changed

+113
-113
lines changed

Some content is hidden

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

47 files changed

+113
-113
lines changed

modules/openapi-json-schema-generator/src/main/resources/python/components/schemas/_helper_enum_class.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ class {{enumInfo.valueToName.jsonPathPiece.camelCase}}:
66

77
@schemas.classproperty
88
{{#eq @key.type "string"}}
9-
def {{this}}(cls) -> str:
9+
def {{this}}(cls) -> typing_extensions.Literal["{{{@key.value}}}"]:
1010
return {{jsonPathPiece.camelCase}}.validate("{{{@key.value}}}")
1111
{{/eq}}
1212
{{#eq @key.type "number"}}
1313
def {{this}}(cls) -> typing.Union[int, float]:
1414
return {{jsonPathPiece.camelCase}}.validate({{{@key.value}}})
1515
{{/eq}}
1616
{{#eq @key.type "integer"}}
17-
def {{this}}(cls) -> int:
17+
def {{this}}(cls) -> typing_extensions.Literal[{{{@key.value}}}]:
1818
return {{jsonPathPiece.camelCase}}.validate({{{@key.value}}})
1919
{{/eq}}
2020
{{#eq @key.type "boolean"}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class EnumWith0DoesNotMatchFalseEnums:
1616

1717
@schemas.classproperty
18-
def POSITIVE_0(cls) -> int:
18+
def POSITIVE_0(cls) -> typing_extensions.Literal[0]:
1919
return EnumWith0DoesNotMatchFalse.validate(0)
2020

2121

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class EnumWith1DoesNotMatchTrueEnums:
1616

1717
@schemas.classproperty
18-
def POSITIVE_1(cls) -> int:
18+
def POSITIVE_1(cls) -> typing_extensions.Literal[1]:
1919
return EnumWith1DoesNotMatchTrue.validate(1)
2020

2121

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
class EnumWithEscapedCharactersEnums:
1616

1717
@schemas.classproperty
18-
def FOO_LINE_FEED_LF_BAR(cls) -> str:
18+
def FOO_LINE_FEED_LF_BAR(cls) -> typing_extensions.Literal["foo\nbar"]:
1919
return EnumWithEscapedCharacters.validate("foo\nbar")
2020

2121
@schemas.classproperty
22-
def FOO_CARRIAGE_RETURN_CR_BAR(cls) -> str:
22+
def FOO_CARRIAGE_RETURN_CR_BAR(cls) -> typing_extensions.Literal["foo\rbar"]:
2323
return EnumWithEscapedCharacters.validate("foo\rbar")
2424

2525

samples/openapi3/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
@@ -15,7 +15,7 @@
1515
class FooEnums:
1616

1717
@schemas.classproperty
18-
def FOO(cls) -> str:
18+
def FOO(cls) -> typing_extensions.Literal["foo"]:
1919
return Foo.validate("foo")
2020

2121

@@ -69,7 +69,7 @@ def validate(
6969
class BarEnums:
7070

7171
@schemas.classproperty
72-
def BAR(cls) -> str:
72+
def BAR(cls) -> typing_extensions.Literal["bar"]:
7373
return Bar.validate("bar")
7474

7575

samples/openapi3/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
@@ -15,7 +15,7 @@
1515
class NulCharactersInStringsEnums:
1616

1717
@schemas.classproperty
18-
def HELLO_NULL_THERE(cls) -> str:
18+
def HELLO_NULL_THERE(cls) -> typing_extensions.Literal["hello\x00there"]:
1919
return NulCharactersInStrings.validate("hello\x00there")
2020

2121

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
class SimpleEnumValidationEnums:
1616

1717
@schemas.classproperty
18-
def POSITIVE_1(cls) -> int:
18+
def POSITIVE_1(cls) -> typing_extensions.Literal[1]:
1919
return SimpleEnumValidation.validate(1)
2020

2121
@schemas.classproperty
22-
def POSITIVE_2(cls) -> int:
22+
def POSITIVE_2(cls) -> typing_extensions.Literal[2]:
2323
return SimpleEnumValidation.validate(2)
2424

2525
@schemas.classproperty
26-
def POSITIVE_3(cls) -> int:
26+
def POSITIVE_3(cls) -> typing_extensions.Literal[3]:
2727
return SimpleEnumValidation.validate(3)
2828

2929

samples/openapi3/client/petstore/python/src/petstore_api/components/schema/basque_pig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class ClassNameEnums:
1616

1717
@schemas.classproperty
18-
def BASQUE_PIG(cls) -> str:
18+
def BASQUE_PIG(cls) -> typing_extensions.Literal["BasquePig"]:
1919
return ClassName.validate("BasquePig")
2020

2121

samples/openapi3/client/petstore/python/src/petstore_api/components/schema/complex_quadrilateral.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class QuadrilateralTypeEnums:
1616

1717
@schemas.classproperty
18-
def COMPLEX_QUADRILATERAL(cls) -> str:
18+
def COMPLEX_QUADRILATERAL(cls) -> typing_extensions.Literal["ComplexQuadrilateral"]:
1919
return QuadrilateralType.validate("ComplexQuadrilateral")
2020

2121

samples/openapi3/client/petstore/python/src/petstore_api/components/schema/currency.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
class CurrencyEnums:
1616

1717
@schemas.classproperty
18-
def EUR(cls) -> str:
18+
def EUR(cls) -> typing_extensions.Literal["eur"]:
1919
return Currency.validate("eur")
2020

2121
@schemas.classproperty
22-
def USD(cls) -> str:
22+
def USD(cls) -> typing_extensions.Literal["usd"]:
2323
return Currency.validate("usd")
2424

2525

0 commit comments

Comments
 (0)