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

Commit 35b5c49

Browse files
authored
Fixes issue 171, fixes required property access (#172)
* Adds property method writing * Adds property acessor and test * Samples regenerated
1 parent 58dfade commit 35b5c49

File tree

61 files changed

+601
-160
lines changed

Some content is hidden

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

61 files changed

+601
-160
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
{{#if requiredProperties}}
2-
32
{{#each requiredProperties}}
43
{{#if @key.isValid}}
54
{{#with this}}
65
{{#if refInfo.refClass}}
7-
{{@key.original}}: '{{> components/schemas/_helper_refclass_with_module }}'
6+
7+
@property
8+
def {{@key.original}}(self) -> '{{> components/schemas/_helper_refclass_with_module }}':
9+
return self.__getitem__("{{{@key.original}}}")
810
{{else}}
911
{{#if jsonPathPiece}}
1012
{{#if schemaIsFromAdditionalProperties}}
11-
{{@key.original}}: Schema_.{{jsonPathPiece.camelCase}}
13+
14+
@property
15+
def {{@key.original}}(self) -> Schema_.{{jsonPathPiece.camelCase}}:
16+
return self.__getitem__("{{{@key.original}}}")
1217
{{else}}
13-
{{@key.original}}: Schema_.Properties.{{jsonPathPiece.camelCase}}
18+
19+
@property
20+
def {{@key.original}}(self) -> Schema_.Properties.{{jsonPathPiece.camelCase}}:
21+
return self.__getitem__("{{{@key.original}}}")
1422
{{/if}}
1523
{{else}}
16-
{{@key.original}}: schemas.AnyTypeSchema
24+
25+
@property
26+
def {{@key.original}}(self) -> schemas.AnyTypeSchema:
27+
return self.__getitem__("{{{@key.original}}}")
1728
{{/if}}
1829
{{/if}}
1930
{{/with}}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,22 +2004,6 @@ class DictBase:
20042004
if not isinstance(self, FileIO):
20052005
raise AttributeError('property setting not supported on immutable instances')
20062006

2007-
def __getattr__(self, name: str):
2008-
"""
2009-
for instance.name access
2010-
Properties are only type hinted for required properties
2011-
so that hasattr(instance, 'optionalProp') is False when that key is not present
2012-
"""
2013-
if not isinstance(self, frozendict.frozendict):
2014-
return super().__getattr__(name)
2015-
if name not in self.__class__.__annotations__:
2016-
raise AttributeError(f"{self} has no attribute '{name}'")
2017-
try:
2018-
value = self[name]
2019-
return value
2020-
except KeyError as ex:
2021-
raise AttributeError(str(ex))
2022-
20232007
def get_item_(self, name: str) -> typing.Union['AnyTypeSchema', Unset]:
20242008
# dict_instance[name] accessor
20252009
if not isinstance(self, frozendict.frozendict):

modules/openapi-json-schema-generator/src/test/resources/3_0/python/petstore_customized.yaml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3273,4 +3273,20 @@ components:
32733273
SelfReferencingArrayModel:
32743274
type: array
32753275
items:
3276-
$ref: '#/components/schemas/SelfReferencingArrayModel'
3276+
$ref: '#/components/schemas/SelfReferencingArrayModel'
3277+
ObjWithRequiredPropsBase:
3278+
type: object
3279+
properties:
3280+
b:
3281+
type: string
3282+
required:
3283+
- b
3284+
ObjWithRequiredProps:
3285+
type: object
3286+
properties:
3287+
a:
3288+
type: string
3289+
required:
3290+
- a
3291+
allOf:
3292+
- $ref: '#/components/schemas/ObjWithRequiredPropsBase'

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class Properties:
5757
}
5858

5959

60-
bar: Schema_.Properties.Bar
60+
@property
61+
def bar(self) -> Schema_.Properties.Bar:
62+
return self.__getitem__("bar")
6163

6264
@typing.overload
6365
def __getitem__(self, name: typing_extensions.Literal["bar"]) -> Schema_.Properties.Bar: ...
@@ -122,7 +124,9 @@ class Properties:
122124
}
123125

124126

125-
foo: Schema_.Properties.Foo
127+
@property
128+
def foo(self) -> Schema_.Properties.Foo:
129+
return self.__getitem__("foo")
126130

127131
@typing.overload
128132
def __getitem__(self, name: typing_extensions.Literal["foo"]) -> Schema_.Properties.Foo: ...

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ class Properties:
6666
}
6767

6868

69-
foo: Schema_.Properties.Foo
69+
@property
70+
def foo(self) -> Schema_.Properties.Foo:
71+
return self.__getitem__("foo")
7072

7173
@typing.overload
7274
def __getitem__(self, name: typing_extensions.Literal["foo"]) -> Schema_.Properties.Foo: ...
@@ -131,7 +133,9 @@ class Properties:
131133
}
132134

133135

134-
baz: Schema_.Properties.Baz
136+
@property
137+
def baz(self) -> Schema_.Properties.Baz:
138+
return self.__getitem__("baz")
135139

136140
@typing.overload
137141
def __getitem__(self, name: typing_extensions.Literal["baz"]) -> Schema_.Properties.Baz: ...
@@ -182,7 +186,9 @@ def __new__(
182186
]
183187

184188

185-
bar: Schema_.Properties.Bar
189+
@property
190+
def bar(self) -> Schema_.Properties.Bar:
191+
return self.__getitem__("bar")
186192

187193
@typing.overload
188194
def __getitem__(self, name: typing_extensions.Literal["bar"]) -> Schema_.Properties.Bar: ...

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class Properties:
5757
}
5858

5959

60-
bar: Schema_.Properties.Bar
60+
@property
61+
def bar(self) -> Schema_.Properties.Bar:
62+
return self.__getitem__("bar")
6163

6264
@typing.overload
6365
def __getitem__(self, name: typing_extensions.Literal["bar"]) -> Schema_.Properties.Bar: ...
@@ -122,7 +124,9 @@ class Properties:
122124
}
123125

124126

125-
foo: Schema_.Properties.Foo
127+
@property
128+
def foo(self) -> Schema_.Properties.Foo:
129+
return self.__getitem__("foo")
126130

127131
@typing.overload
128132
def __getitem__(self, name: typing_extensions.Literal["foo"]) -> Schema_.Properties.Foo: ...

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def BAR(cls):
8181
"bar": Bar,
8282
}
8383

84-
bar: Schema_.Properties.Bar
84+
@property
85+
def bar(self) -> Schema_.Properties.Bar:
86+
return self.__getitem__("bar")
8587

8688
@typing.overload
8789
def __getitem__(self, name: typing_extensions.Literal["bar"]) -> Schema_.Properties.Bar: ...

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class Properties:
5757
}
5858

5959

60-
bar: Schema_.Properties.Bar
60+
@property
61+
def bar(self) -> Schema_.Properties.Bar:
62+
return self.__getitem__("bar")
6163

6264
@typing.overload
6365
def __getitem__(self, name: typing_extensions.Literal["bar"]) -> Schema_.Properties.Bar: ...
@@ -122,7 +124,9 @@ class Properties:
122124
}
123125

124126

125-
foo: Schema_.Properties.Foo
127+
@property
128+
def foo(self) -> Schema_.Properties.Foo:
129+
return self.__getitem__("foo")
126130

127131
@typing.overload
128132
def __getitem__(self, name: typing_extensions.Literal["foo"]) -> Schema_.Properties.Foo: ...

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ class Schema_:
5454
}
5555

5656

57-
bar: schemas.AnyTypeSchema
58-
foo: schemas.AnyTypeSchema
57+
@property
58+
def bar(self) -> schemas.AnyTypeSchema:
59+
return self.__getitem__("bar")
60+
61+
@property
62+
def foo(self) -> schemas.AnyTypeSchema:
63+
return self.__getitem__("foo")
5964

6065
@typing.overload
6166
def __getitem__(self, name: typing_extensions.Literal["bar"]) -> schemas.AnyTypeSchema: ...
@@ -123,8 +128,13 @@ class Schema_:
123128
}
124129

125130

126-
baz: schemas.AnyTypeSchema
127-
foo: schemas.AnyTypeSchema
131+
@property
132+
def baz(self) -> schemas.AnyTypeSchema:
133+
return self.__getitem__("baz")
134+
135+
@property
136+
def foo(self) -> schemas.AnyTypeSchema:
137+
return self.__getitem__("foo")
128138

129139
@typing.overload
130140
def __getitem__(self, name: typing_extensions.Literal["baz"]) -> schemas.AnyTypeSchema: ...

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class Properties:
4848
}
4949

5050

51-
foo: Schema_.Properties.Foo
51+
@property
52+
def foo(self) -> Schema_.Properties.Foo:
53+
return self.__getitem__("foo")
5254

5355
@typing.overload
5456
def __getitem__(self, name: typing_extensions.Literal["foo"]) -> Schema_.Properties.Foo: ...

0 commit comments

Comments
 (0)