datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Request: Include default or HTTP status code in class name for Response models

Open joshuathompsonlindley opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. I'm using datamodel-code-generator to generate Python dataclasses from an OpenAPI model. These models have one default response and at least one response using the HTTP status code pattern:

responses:
    default:
         ...
    200:
         ...

Then when running the code generator I would get names like

ResourceGetResponse - for the default response (usually an error) ResourceGetResponse1 - for the 200 status response

This might be potentially confusing for consumers of these dataclasses, who blindly import ResourceGetResponse expecting that this corresponds to the 200 status response. We use these dataclasses in our data validation code so this could cause some confusion.

Describe the solution you'd like Change the code generation to consider the 'current' response in the class name of the model, so instead of:

ResourceGetResponse - for the default response (usually an error) ResourceGetResponse1 - for the 200 status response

The code generator would produce:

ResourceGetResponseDefault - for the default response (usually an error) ResourceGetResponse200 - for the 200 status response

Describe alternatives you've considered An alternative would be to edit this manually or after generation time, but we're updating these models quite frequently as we're in development of this new solution.

Additional context Add any other context or screenshots about the feature request here.

joshuathompsonlindley avatar Jan 31 '24 15:01 joshuathompsonlindley

I'm sorry for the late reply. Thanks for the great pointers. I feel like it's too big an effect on existing code, but it might not be a bad idea to make your suggestion the behavior rather than doing numbering that makes no sense.

koxudaxi avatar Mar 16 '24 16:03 koxudaxi