Skip to content

Generator fails to build models for schemas of type object with no/empty properties #25

@micolous

Description

@micolous

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions