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

Commit 834b21b

Browse files
authored
Refactors schema class templates (#217)
* Breaks up composed schema template * Removes dict partial * Partial sample regen * Switches dict schema info order back to original * Refactos some lines into const and enum templates * Template tweaks * Extracts unique items template * Creates max_length template * Extracts template for minLength * Simplifies schema_cls template * Adds max and min items templates * Makes and uses templates for max/minProperties * Adds and uses max and min templates * Removes validation template * Deprecates hasValidation, adds default and format templates * Adds and uses template for boolean schema class * Adds templates for number and string * Renames template to _schema_anytype_or_multitype * Samples and docs regenerated * Changes min max value detection to use not equal to null * Fixes bug where extra const was written * Samples regen
1 parent b04e9f3 commit 834b21b

Some content is hidden

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

53 files changed

+429
-279
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,3 @@ class AnyofWithBaseSchema(
4747
str,
4848
})
4949
any_of: AnyOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(AnyOf)) # type: ignore
50-

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

-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,3 @@ class OneofWithBaseSchema(
4747
str,
4848
})
4949
one_of: OneOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(OneOf)) # type: ignore
50-

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,14 @@ class _1(
178178

179179
@dataclasses.dataclass(frozen=True)
180180
class OneofWithRequired(
181-
schemas.Schema[schemas.immutabledict, tuple]
181+
schemas.Schema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], tuple]
182182
):
183183
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
184184
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
185185
186186
Do not edit the class manually.
187187
"""
188-
types: typing.FrozenSet[typing.Type] = frozenset({
189-
schemas.immutabledict,
190-
})
188+
types: typing.FrozenSet[typing.Type] = frozenset({schemas.immutabledict})
191189
one_of: OneOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(OneOf)) # type: ignore
192190

193191
@classmethod

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

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class Alpha(
2121
int,
2222
})
2323
inclusive_maximum: typing.Union[int, float] = 3
24-
default: typing.Union[int, float] = 5
2524
Properties = typing.TypedDict(
2625
'Properties',
2726
{

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

+1-14
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,4 @@
1010
from __future__ import annotations
1111
from unit_test_api.shared_imports.schema_imports import * # pyright: ignore [reportWildcardImportFromLibrary]
1212

13-
14-
15-
@dataclasses.dataclass(frozen=True)
16-
class UniqueitemsFalseValidation(
17-
schemas.AnyTypeSchema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], typing.Tuple[schemas.OUTPUT_BASE_TYPES, ...]],
18-
):
19-
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
20-
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
21-
22-
Do not edit the class manually.
23-
"""
24-
# any type
25-
unique_items: bool = False
26-
13+
UniqueitemsFalseValidation: typing_extensions.TypeAlias = schemas.AnyTypeSchema

samples/client/petstore/python/src/petstore_api/components/schema/abstract_step_message.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ class AbstractStepMessage(
101101
102102
Abstract Step
103103
"""
104-
types: typing.FrozenSet[typing.Type] = frozenset({
105-
schemas.immutabledict,
106-
})
104+
types: typing.FrozenSet[typing.Type] = frozenset({schemas.immutabledict})
107105
required: typing.FrozenSet[str] = frozenset({
108106
"description",
109107
"discriminator",
@@ -123,7 +121,7 @@ class AbstractStepMessage(
123121
typing.Type
124122
] = dataclasses.field(
125123
default_factory=lambda: {
126-
schemas.immutabledict: AbstractStepMessageDict,
124+
schemas.immutabledict: AbstractStepMessageDict
127125
}
128126
)
129127

samples/client/petstore/python/src/petstore_api/components/schema/additional_properties_validator.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,14 @@ def validate(
257257

258258
@dataclasses.dataclass(frozen=True)
259259
class AdditionalPropertiesValidator(
260-
schemas.Schema[schemas.immutabledict, tuple]
260+
schemas.Schema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], tuple]
261261
):
262262
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
263263
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
264264
265265
Do not edit the class manually.
266266
"""
267-
types: typing.FrozenSet[typing.Type] = frozenset({
268-
schemas.immutabledict,
269-
})
267+
types: typing.FrozenSet[typing.Type] = frozenset({schemas.immutabledict})
270268
all_of: AllOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(AllOf)) # type: ignore
271269

272270
@classmethod

samples/client/petstore/python/src/petstore_api/components/schema/composed_bool.py

-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@ class ComposedBool(
2929
schemas.Bool,
3030
})
3131
all_of: AllOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(AllOf)) # type: ignore
32-

samples/client/petstore/python/src/petstore_api/components/schema/composed_none.py

-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@ class ComposedNone(
2929
type(None),
3030
})
3131
all_of: AllOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(AllOf)) # type: ignore
32-

samples/client/petstore/python/src/petstore_api/components/schema/composed_number.py

-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ class ComposedNumber(
3030
int,
3131
})
3232
all_of: AllOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(AllOf)) # type: ignore
33-

samples/client/petstore/python/src/petstore_api/components/schema/composed_object.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818

1919
@dataclasses.dataclass(frozen=True)
2020
class ComposedObject(
21-
schemas.Schema[schemas.immutabledict, tuple]
21+
schemas.Schema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], tuple]
2222
):
2323
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
2424
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
2525
2626
Do not edit the class manually.
2727
"""
28-
types: typing.FrozenSet[typing.Type] = frozenset({
29-
schemas.immutabledict,
30-
})
28+
types: typing.FrozenSet[typing.Type] = frozenset({schemas.immutabledict})
3129
all_of: AllOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(AllOf)) # type: ignore
3230

3331
@classmethod

samples/client/petstore/python/src/petstore_api/components/schema/composed_string.py

-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@ class ComposedString(
2929
str,
3030
})
3131
all_of: AllOf = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance(AllOf)) # type: ignore
32-

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

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class IntegerEnumWithDefaultValue(
4040
int,
4141
})
4242
format: str = 'int'
43-
default: typing.Literal[0] = 0
4443
enum_value_to_name: typing.Mapping[typing.Union[int, float, str, schemas.Bool, None], str] = dataclasses.field(
4544
default_factory=lambda: {
4645
0: "POSITIVE_0",

samples/client/petstore/python/src/petstore_api/components/schema/obj_with_required_props.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ class ObjWithRequiredProps(
7373
7474
Do not edit the class manually.
7575
"""
76-
types: typing.FrozenSet[typing.Type] = frozenset({
77-
schemas.immutabledict,
78-
})
76+
types: typing.FrozenSet[typing.Type] = frozenset({schemas.immutabledict})
7977
required: typing.FrozenSet[str] = frozenset({
8078
"a",
8179
})
@@ -86,7 +84,7 @@ class ObjWithRequiredProps(
8684
typing.Type
8785
] = dataclasses.field(
8886
default_factory=lambda: {
89-
schemas.immutabledict: ObjWithRequiredPropsDict,
87+
schemas.immutabledict: ObjWithRequiredPropsDict
9088
}
9189
)
9290

samples/client/petstore/python/src/petstore_api/components/schema/order.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,7 @@ def validate(
9696
],
9797
validated_arg
9898
)
99-
100-
101-
@dataclasses.dataclass(frozen=True)
102-
class Complete(
103-
schemas.BoolSchema
104-
):
105-
types: typing.FrozenSet[typing.Type] = frozenset({
106-
schemas.Bool,
107-
})
108-
default: typing.Literal[False] = False
99+
Complete: typing_extensions.TypeAlias = schemas.BoolSchema
109100
Properties = typing.TypedDict(
110101
'Properties',
111102
{

samples/client/petstore/python/src/petstore_api/components/schema/parent_pet.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414

1515
@dataclasses.dataclass(frozen=True)
1616
class ParentPet(
17-
schemas.Schema[schemas.immutabledict, tuple]
17+
schemas.Schema[schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES], tuple]
1818
):
1919
"""NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
2020
Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
2121
2222
Do not edit the class manually.
2323
"""
24-
types: typing.FrozenSet[typing.Type] = frozenset({
25-
schemas.immutabledict,
26-
})
24+
types: typing.FrozenSet[typing.Type] = frozenset({schemas.immutabledict})
2725
discriminator: typing.Mapping[str, typing.Mapping[str, typing.Type[schemas.Schema]]] = dataclasses.field(
2826
default_factory=lambda: {
2927
'pet_type': {

src/main/java/org/openapijsonschematools/codegen/common/ModelUtils.java

+4
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@ public static boolean isModelWithPropertiesOnly(Schema schema) {
689689
return false;
690690
}
691691

692+
/*
693+
Remove this in the 4.0.0 release because it is unused
694+
*/
695+
@Deprecated
692696
public static boolean hasValidation(Schema sc) {
693697
return (
694698
sc.getMaxItems() != null ||

src/main/java/org/openapijsonschematools/codegen/generators/openapimodels/CodegenSchema.java

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public class CodegenSchema {
112112
public String pathFromDocRoot = null;
113113
public boolean isInline = false;
114114

115+
/*
116+
Remove this in the 4.0.0 release because it is unused
117+
*/
118+
@Deprecated
115119
public boolean hasValidation() {
116120
return maxItems != null || minItems != null || minProperties != null || maxProperties != null || minLength != null || maxLength != null || multipleOf != null || patternInfo != null || minimum != null || maximum != null || exclusiveMinimum != null || exclusiveMaximum != null || uniqueItems != null;
117121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{#with additionalProperties}}
2+
additional_properties: typing.Type[{{#if refInfo.refClass}}{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}{{else}}{{jsonPathPiece.camelCase}}{{/if}}] = dataclasses.field(default_factory=lambda: {{#if refInfo.refClass}}{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}{{else}}{{jsonPathPiece.camelCase}}{{/if}}) # type: ignore
3+
{{/with}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
all_of: {{allOf.jsonPathPiece.camelCase}} = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance({{allOf.jsonPathPiece.camelCase}})) # type: ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
any_of: {{anyOf.jsonPathPiece.camelCase}} = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance({{anyOf.jsonPathPiece.camelCase}})) # type: ignore

src/main/resources/python/components/schemas/schema_cls/_composed_schemas.hbs

-14
This file was deleted.

src/main/resources/python/components/schemas/schema_cls/_const.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ const_value_to_name: typing.Mapping[typing.Union[int, float, str, schemas.Bool,
2323
{{/each}}
2424
}
2525
)
26+
const = {{constInfo.jsonPathPiece.camelCase}}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
{{#if contains}}
2-
{{#with contains}}
1+
{{#with contains}}
32
contains: typing.Type[{{#if refInfo.refClass}}{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}{{else}}{{jsonPathPiece.camelCase}}{{/if}}] = dataclasses.field(default_factory=lambda: {{#if refInfo.refClass}}{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}{{else}}{{jsonPathPiece.camelCase}}{{/if}}) # type: ignore
4-
{{/with}}
5-
{{#if maxContains}}
3+
{{/with}}
4+
{{#neq maxContains null}}
65
max_contains: typing.Literal[{{maxContains}}] = {{maxContains}}
7-
{{/if}}
8-
{{#if minContains}}
6+
{{/neq}}
7+
{{#neq minContains null}}
98
min_contains: typing.Literal[{{minContains}}] = {{minContains}}
10-
{{/if}}
11-
{{/if}}
9+
{{/neq}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{{#with defaultValue}}
2+
{{#eq type "string"}}
3+
default: typing.Literal["{{{value}}}"] = "{{{value}}}"
4+
{{/eq}}
5+
{{#eq type "number"}}
6+
default: typing.Union[int, float] = {{{value}}}
7+
{{/eq}}
8+
{{#eq type "integer"}}
9+
default: typing.Literal[{{{value}}}] = {{{value}}}
10+
{{/eq}}
11+
{{#eq type "boolean"}}
12+
{{#if value}}
13+
default: typing.Literal[True] = True
14+
{{else}}
15+
default: typing.Literal[False] = False
16+
{{/if}}
17+
{{/eq}}
18+
{{#eq type "null"}}
19+
default: None = None
20+
{{/eq}}
21+
{{/with}}

src/main/resources/python/components/schemas/schema_cls/_dict_partial.hbs

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{#with discriminator}}
2+
discriminator: typing.Mapping[str, typing.Mapping[str, typing.Type[schemas.Schema]]] = dataclasses.field(
3+
default_factory=lambda: {
4+
'{{{propertyName.original}}}': {
5+
{{#each mappedModels}}
6+
'{{mappingName}}': {{{modelName}}},
7+
{{/each}}
8+
}
9+
}
10+
)
11+
{{/with}}

src/main/resources/python/components/schemas/schema_cls/_enum.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ enum_value_to_name: typing.Mapping[typing.Union[int, float, str, schemas.Bool, N
2323
{{/each}}
2424
}
2525
)
26+
enums = {{enumInfo.jsonPathPiece.camelCase}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
format: str = '{{format}}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
max_items: int = {{maxItems}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
max_length: int = {{maxLength}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
max_properties: int = {{maxProperties}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#if exclusiveMaximum}}exclusive_maximum{{/if}}inclusive_maximum{{#unless exclusiveMaximum}}{{/unless}}: typing.Union[int, float] = {{maximum}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
min_items: int = {{minItems}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
min_length: int = {{minLength}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
min_properties: int = {{minProperties}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#if exclusiveMinimum}}exclusive_minimum{{/if}}inclusive_minimum{{#unless exclusiveMinimum}}{{/unless}}: typing.Union[int, float] = {{minimum}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
multiple_of: typing.Union[int, float] = {{multipleOf}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{#with not}}
2+
not_: typing.Type[{{#if refInfo.refClass}}{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}{{else}}{{jsonPathPiece.camelCase}}{{/if}}] = dataclasses.field(default_factory=lambda: {{#if refInfo.refClass}}{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}{{else}}{{jsonPathPiece.camelCase}}{{/if}}) # type: ignore
3+
{{/with}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
one_of: {{oneOf.jsonPathPiece.camelCase}} = dataclasses.field(default_factory=lambda: schemas.tuple_to_instance({{oneOf.jsonPathPiece.camelCase}})) # type: ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
pattern: schemas.PatternInfo = schemas.PatternInfo(
2+
{{#with patternInfo}}
3+
pattern=r'{{{pattern}}}'{{#if flags}},{{/if}} # noqa: E501
4+
{{#if flags}}
5+
{{#eq flags.size 1}}
6+
flags={{#each flags}}re.{{#eq this "i"}}I{{/eq}}{{#eq this "m"}}M{{/eq}}{{#eq this "s"}}S{{/eq}}{{#eq this "u"}}U{{/eq}}{{/each}},
7+
{{else}}
8+
flags=(
9+
{{#each flags}}
10+
{{#eq this "i"}}
11+
re.I{{#unless @last}} |{{/unless}}
12+
{{/eq}}
13+
{{#eq this "m"}}
14+
re.M{{#unless @last}} |{{/unless}}
15+
{{/eq}}
16+
{{#eq this "s"}}
17+
re.S{{#unless @last}} |{{/unless}}
18+
{{/eq}}
19+
{{#eq this "u"}}
20+
re.U{{#unless @last}} |{{/unless}}
21+
{{/eq}}
22+
{{/each}}
23+
)
24+
{{/eq}}
25+
{{/if}}
26+
{{/with}}
27+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
properties: {{properties.jsonPathPiece.camelCase}} = dataclasses.field(default_factory=lambda: schemas.typed_dict_to_instance({{properties.jsonPathPiece.camelCase}})) # type: ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
required: typing.FrozenSet[str] = frozenset({
2+
{{#each requiredProperties}}
3+
"{{{@key.original}}}",
4+
{{/each}}
5+
})

0 commit comments

Comments
 (0)