-
Notifications
You must be signed in to change notification settings - Fork 8
Description
The generator fails to build code or generates invalid code for a schemas that describe an empty object
.
Build failure with no properties
:
python-client-generator
fails to build a model for an object
with no properties
key, eg:
{
"components": {
"schemas": {
"EmptyObject": {
"type": "object"
}
}
}
}
Generating a client when properties
is missing makes the generator raise KeyError('properties')
at:
python-client-generator/python_client_generator/generate_models.py
Lines 99 to 102 in 05b2c51
else: | |
# Must have properties | |
for p_schema in model["properties"].values(): | |
refs += _get_schema_references(p_schema) |
Contrary to the code comment, in the generator, it is actually valid for an object to have no properties field. Per JSON Schema Core specification:
The value of "properties" MUST be an object. Each value of this object MUST be a valid JSON Schema.
...
Omitting this keyword has the same assertion behavior as an empty object.
Invalid code with empty properties
For an object
with an empty properties
key, eg:
{
"components": {
"schemas": {
"EmptyObject": {
"type": "object",
"properties": {}
}
}
}
}
The generator builds invalid Python code:
class EmptyObject(BaseModel):
There should be a pass
in there when there are no fields.
Testing the fix
It looks like FastAPI always includes a properties
key for empty objects (so only hits the second issue), whereas protoc-gen-openapiv2
(both before and after converting OAS v2 to OAS v3) does not (so hits both issues).
I've modified both the Pet Store samples to trigger both cases in #26.