Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/artsmolin/pythogen
Browse files Browse the repository at this point in the history
  • Loading branch information
artsmolin committed Jul 18, 2022
2 parents 3948e1d + 85f1322 commit 17b51cb
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pythogen/parsers/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _parse_type(self, data: Dict[str, Any]) -> models.Type:
try:
data_type = models.Type(raw_data_type)
except ValueError:
raise Exception(f'Unable to parse schema "{id}", unknown type "{raw_data_type}"')
raise Exception(f'Unable to parse schema "{id}", unknown type "{raw_data_type}" on "{data}"')
return data_type

def _parse_format(self, data: Dict[str, Any]) -> Optional[models.Format]:
Expand Down Expand Up @@ -183,6 +183,11 @@ def _parse_properties(
# specify inline array name
property_schema_id = key + "_list"
schema = self.parse_item(property_schema_id, property_schema_data)
elif 'anyOf' in property_schema_data:
# extract inline object definition to schema
property_schema_id = key + "_obj"
schema = self.parse_item(property_schema_id, property_schema_data)
self._inline_schema_aggregator.add(property_schema_id, schema)
else:
property_schema_id = f'<inline+{models.SchemaObject.__name__}>'
schema = self.parse_item(property_schema_id, property_schema_data)
Expand Down
2 changes: 1 addition & 1 deletion pythogen/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def j2_typerepr(schema: models.SchemaObject) -> str:

elif schema.type == models.Type.array and schema.items:
if schema.items.type is models.Type.any_of: # type: ignore
items = [classname(item.schema.id) for item in schema.items.items] # type: ignore
items = [classname(item.id) for item in schema.items.items] # type: ignore
representation = f'List[Union{items}]'
else:
item = j2_typerepr(schema.items) # type: ignore
Expand Down
36 changes: 36 additions & 0 deletions tests/clients/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ class GetObjectWithInlineArrayResponse200Item(BaseModel):
# optional ---


class AnimalObj(BaseModel):
"""
None
"""
__root__: Union[
'Cat',
'Dog',
]


class TierObj(BaseModel):
"""
None
Expand Down Expand Up @@ -481,6 +491,28 @@ def change_name(cls, values):
return values


class Dog(BaseModel):
"""
Dog
"""

# required ---

# optional ---
name: Optional[str] = None


class Cat(BaseModel):
"""
Cat
"""

# required ---

# optional ---
name: Optional[str] = None


class GetObjectResp(BaseModel):
"""
GetObjectResp
Expand All @@ -494,6 +526,7 @@ class GetObjectResp(BaseModel):
array_data: Optional[List[str]] = None
boolean_data: Optional[bool] = None
tier: Optional[TierObj] = None
animal: Optional[AnimalObj] = None


class Data(BaseModel):
Expand Down Expand Up @@ -1312,6 +1345,7 @@ def add_tracing_data_to_headers(self, headers_: Dict[str, str]) -> None:
RewardsListItem.update_forward_refs()
GetObjectWithInlineArrayResponse200.update_forward_refs()
GetObjectWithInlineArrayResponse200Item.update_forward_refs()
AnimalObj.update_forward_refs()
TierObj.update_forward_refs()
GetObjectNoRefSchemaResponse200.update_forward_refs()
TestSafetyKey.update_forward_refs()
Expand All @@ -1324,6 +1358,8 @@ def add_tracing_data_to_headers(self, headers_: Dict[str, str]) -> None:
PutObjectData.update_forward_refs()
PatchObjectData.update_forward_refs()
PostObjectData.update_forward_refs()
Dog.update_forward_refs()
Cat.update_forward_refs()
GetObjectResp.update_forward_refs()
Data.update_forward_refs()
AllOfResp.update_forward_refs()
36 changes: 36 additions & 0 deletions tests/clients/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ class GetObjectWithInlineArrayResponse200Item(BaseModel):
# optional ---


class AnimalObj(BaseModel):
"""
None
"""
__root__: Union[
'Cat',
'Dog',
]


class TierObj(BaseModel):
"""
None
Expand Down Expand Up @@ -481,6 +491,28 @@ def change_name(cls, values):
return values


class Dog(BaseModel):
"""
Dog
"""

# required ---

# optional ---
name: Optional[str] = None


class Cat(BaseModel):
"""
Cat
"""

# required ---

# optional ---
name: Optional[str] = None


class GetObjectResp(BaseModel):
"""
GetObjectResp
Expand All @@ -494,6 +526,7 @@ class GetObjectResp(BaseModel):
array_data: Optional[List[str]] = None
boolean_data: Optional[bool] = None
tier: Optional[TierObj] = None
animal: Optional[AnimalObj] = None


class Data(BaseModel):
Expand Down Expand Up @@ -1312,6 +1345,7 @@ def add_tracing_data_to_headers(self, headers_: Dict[str, str]) -> None:
RewardsListItem.update_forward_refs()
GetObjectWithInlineArrayResponse200.update_forward_refs()
GetObjectWithInlineArrayResponse200Item.update_forward_refs()
AnimalObj.update_forward_refs()
TierObj.update_forward_refs()
GetObjectNoRefSchemaResponse200.update_forward_refs()
TestSafetyKey.update_forward_refs()
Expand All @@ -1324,6 +1358,8 @@ def add_tracing_data_to_headers(self, headers_: Dict[str, str]) -> None:
PutObjectData.update_forward_refs()
PatchObjectData.update_forward_refs()
PostObjectData.update_forward_refs()
Dog.update_forward_refs()
Cat.update_forward_refs()
GetObjectResp.update_forward_refs()
Data.update_forward_refs()
AllOfResp.update_forward_refs()
21 changes: 21 additions & 0 deletions tests/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,22 @@ components:
title: Data
type: integer
additionalProperties: false
Cat:
title: Cat
type: object
properties:
name:
title: name
type: string
additionalProperties: false
Dog:
title: Dog
type: object
properties:
name:
title: name
type: string
additionalProperties: false
GetObjectResp:
title: GetObjectResp
type: object
Expand Down Expand Up @@ -448,6 +464,11 @@ components:
type: string
priority:
type: integer
animal:
type: object
anyOf:
- {$ref: '#/components/schemas/Cat'}
- {$ref: '#/components/schemas/Dog'}
additionalProperties: false
PostObjectData:
title: PostObjectData
Expand Down

0 comments on commit 17b51cb

Please sign in to comment.