You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The python-fastapi generator generates invalid model code when the model's OpenAPI schema specifies additionalProperties:
It lets the generated model class extend the type of the value of additionalProperties.
Also, it generates an additional_properties dict field to hold the additional properties but the generated to_dict method that would map it to the correct transfer representation is not honoured by pydantic v2 (which supports storing additional properties within the model object's __dict__ when model_config.extra=allow is specified).
For instance currently the generator generates the following model code when given a type ExampleObject that specifies a static name property as well as additionalProperties: {"type": "string"}:
It lets the model class inherit from str (specified by additionalProperties schema) and import the non-existing package example.gen.models.str.
It generated an additional_properties dict in addition to the __dict__ that already holds all model fields dynamically under the hood - the latter should be leveraged and is sufficient.
Expectation
Instead, I'd expect the following:
It should not inherit the value type specified within the additionalProperties schema.
It should not generate an additional additional_properties dict but leverage the built-in __dict__ which is anyway how pydantic v2 supports additional properties.
openapi: "3.0.1"info:
title: Example APIdescription: Example API to test additionalProperties supportversion: 1.0.0servers:
- url: /tags:
- name: examplepaths:
/example:
get:
operationId: examplesummary: Example operationdescription: An operation that returns an object with dynamic propertiestags:
- exampleresponses:
'200':
description: Example objectcontent:
application/json:
schema:
$ref: '#/components/schemas/ExampleObject'components:
schemas:
ExampleObject:
type: objectproperties:
name:
type: stringadditionalProperties:
type: stringrequired:
- name
Generation Details
Generate a python server from the previously listed openapi.yaml file using the python-fastapi generator:
Bug Report Checklist
Description
The
python-fastapi
generator generates invalid model code when the model's OpenAPI schema specifiesadditionalProperties
:It lets the generated model class extend the type of the value of
additionalProperties
.Also, it generates an
additional_properties
dict field to hold the additional properties but the generatedto_dict
method that would map it to the correct transfer representation is not honoured by pydantic v2 (which supports storing additional properties within the model object's__dict__
whenmodel_config.extra=allow
is specified).For instance currently the generator generates the following model code when given a type
ExampleObject
that specifies a staticname
property as well asadditionalProperties: {"type": "string"}
:example_object.py
Please note:
str
(specified byadditionalProperties
schema) and import the non-existing packageexample.gen.models.str
.additional_properties
dict in addition to the__dict__
that already holds all model fields dynamically under the hood - the latter should be leveraged and is sufficient.Expectation
Instead, I'd expect the following:
additionalProperties
schema.additional_properties
dict but leverage the built-in__dict__
which is anyway how pydantic v2 supports additional properties.openapi-generator version
131fd51 # v7.7.0+patch
OpenAPI declaration file content or url
openapi.yaml with additionalProperties schema
Generation Details
Generate a python server from the previously listed
openapi.yaml
file using thepython-fastapi
generator:Steps to reproduce
Related issues/PRs
Suggest a fix
The text was updated successfully, but these errors were encountered: