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

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

+2-2
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

+1-1
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

+1-1
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

+2-2
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

+2-2
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

+1-1
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

+3-3
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

+1-1
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

+1-1
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

+2-2
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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class ClassNameEnums:
1616

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

2121

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
class JustSymbolEnums:
1616

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

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

2525

@@ -83,11 +83,11 @@ def validate(
8383
class ItemsEnums:
8484

8585
@schemas.classproperty
86-
def FISH(cls) -> str:
86+
def FISH(cls) -> typing_extensions.Literal["fish"]:
8787
return Items.validate("fish")
8888

8989
@schemas.classproperty
90-
def CRAB(cls) -> str:
90+
def CRAB(cls) -> typing_extensions.Literal["crab"]:
9191
return Items.validate("crab")
9292

9393

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@
1515
class EnumClassEnums:
1616

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

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

2525
@schemas.classproperty
26-
def LEFT_PARENTHESIS_XYZ_RIGHT_PARENTHESIS(cls) -> str:
26+
def LEFT_PARENTHESIS_XYZ_RIGHT_PARENTHESIS(cls) -> typing_extensions.Literal["(xyz)"]:
2727
return EnumClass.validate("(xyz)")
2828

2929
@schemas.classproperty
30-
def COUNT_1M(cls) -> str:
30+
def COUNT_1M(cls) -> typing_extensions.Literal["COUNT_1M"]:
3131
return EnumClass.validate("COUNT_1M")
3232

3333
@schemas.classproperty
34-
def COUNT_50M(cls) -> str:
34+
def COUNT_50M(cls) -> typing_extensions.Literal["COUNT_50M"]:
3535
return EnumClass.validate("COUNT_50M")
3636

3737

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
class EnumStringEnums:
1616

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

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

2525
@schemas.classproperty
26-
def EMPTY(cls) -> str:
26+
def EMPTY(cls) -> typing_extensions.Literal[""]:
2727
return EnumString.validate("")
2828

2929

@@ -97,15 +97,15 @@ def validate(
9797
class EnumStringRequiredEnums:
9898

9999
@schemas.classproperty
100-
def UPPER(cls) -> str:
100+
def UPPER(cls) -> typing_extensions.Literal["UPPER"]:
101101
return EnumStringRequired.validate("UPPER")
102102

103103
@schemas.classproperty
104-
def LOWER(cls) -> str:
104+
def LOWER(cls) -> typing_extensions.Literal["lower"]:
105105
return EnumStringRequired.validate("lower")
106106

107107
@schemas.classproperty
108-
def EMPTY(cls) -> str:
108+
def EMPTY(cls) -> typing_extensions.Literal[""]:
109109
return EnumStringRequired.validate("")
110110

111111

@@ -179,11 +179,11 @@ def validate(
179179
class EnumIntegerEnums:
180180

181181
@schemas.classproperty
182-
def POSITIVE_1(cls) -> int:
182+
def POSITIVE_1(cls) -> typing_extensions.Literal[1]:
183183
return EnumInteger.validate(1)
184184

185185
@schemas.classproperty
186-
def NEGATIVE_1(cls) -> int:
186+
def NEGATIVE_1(cls) -> typing_extensions.Literal[-1]:
187187
return EnumInteger.validate(-1)
188188

189189

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class TriangleTypeEnums:
1616

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

2121

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
class IntegerEnumEnums:
1616

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

2121
@schemas.classproperty
22-
def POSITIVE_1(cls) -> int:
22+
def POSITIVE_1(cls) -> typing_extensions.Literal[1]:
2323
return IntegerEnum.validate(1)
2424

2525
@schemas.classproperty
26-
def POSITIVE_2(cls) -> int:
26+
def POSITIVE_2(cls) -> typing_extensions.Literal[2]:
2727
return IntegerEnum.validate(2)
2828

2929

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
class IntegerEnumBigEnums:
1616

1717
@schemas.classproperty
18-
def POSITIVE_10(cls) -> int:
18+
def POSITIVE_10(cls) -> typing_extensions.Literal[10]:
1919
return IntegerEnumBig.validate(10)
2020

2121
@schemas.classproperty
22-
def POSITIVE_11(cls) -> int:
22+
def POSITIVE_11(cls) -> typing_extensions.Literal[11]:
2323
return IntegerEnumBig.validate(11)
2424

2525
@schemas.classproperty
26-
def POSITIVE_12(cls) -> int:
26+
def POSITIVE_12(cls) -> typing_extensions.Literal[12]:
2727
return IntegerEnumBig.validate(12)
2828

2929

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class IntegerEnumOneValueEnums:
1616

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

2121

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
class IntegerEnumWithDefaultValueEnums:
1616

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

2121
@schemas.classproperty
22-
def POSITIVE_1(cls) -> int:
22+
def POSITIVE_1(cls) -> typing_extensions.Literal[1]:
2323
return IntegerEnumWithDefaultValue.validate(1)
2424

2525
@schemas.classproperty
26-
def POSITIVE_2(cls) -> int:
26+
def POSITIVE_2(cls) -> typing_extensions.Literal[2]:
2727
return IntegerEnumWithDefaultValue.validate(2)
2828

2929

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class TriangleTypeEnums:
1616

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

2121

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
class OpEnums:
1919

2020
@schemas.classproperty
21-
def ADD(cls) -> str:
21+
def ADD(cls) -> typing_extensions.Literal["add"]:
2222
return Op.validate("add")
2323

2424
@schemas.classproperty
25-
def REPLACE(cls) -> str:
25+
def REPLACE(cls) -> typing_extensions.Literal["replace"]:
2626
return Op.validate("replace")
2727

2828
@schemas.classproperty
29-
def TEST(cls) -> str:
29+
def TEST(cls) -> typing_extensions.Literal["test"]:
3030
return Op.validate("test")
3131

3232

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
class OpEnums:
1919

2020
@schemas.classproperty
21-
def MOVE(cls) -> str:
21+
def MOVE(cls) -> typing_extensions.Literal["move"]:
2222
return Op.validate("move")
2323

2424
@schemas.classproperty
25-
def COPY(cls) -> str:
25+
def COPY(cls) -> typing_extensions.Literal["copy"]:
2626
return Op.validate("copy")
2727

2828

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class OpEnums:
1818

1919
@schemas.classproperty
20-
def REMOVE(cls) -> str:
20+
def REMOVE(cls) -> typing_extensions.Literal["remove"]:
2121
return Op.validate("remove")
2222

2323

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ def validate(
132132
class AdditionalPropertiesEnums:
133133

134134
@schemas.classproperty
135-
def UPPER(cls) -> str:
135+
def UPPER(cls) -> typing_extensions.Literal["UPPER"]:
136136
return AdditionalProperties3.validate("UPPER")
137137

138138
@schemas.classproperty
139-
def LOWER(cls) -> str:
139+
def LOWER(cls) -> typing_extensions.Literal["lower"]:
140140
return AdditionalProperties3.validate("lower")
141141

142142

0 commit comments

Comments
 (0)