django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

Use serialization aliases for responses in OpenAPI schema

Open camuthig opened this issue 1 year ago • 8 comments
trafficstars

When generating JSON schemas from the Pydantic types, the mode must be set in order for the correct aliases to be used. The default is to generate the schema for validation mode, which means that for building response schemas, the mode needs to be explicitly set to serialization.

This fixes issues I brought up in discussion #1132

camuthig avatar May 11 '24 22:05 camuthig

This might be the same thing as https://github.com/vitalik/django-ninja/pull/1139

pmdevita avatar May 14 '24 15:05 pmdevita

@pmdevita thanks for calling that out. There is some duplication here in that both pulls depend on leveraging the mode argument in model_json_schema. However, the other issue/pull request are also tackling a more nuanced issue around definition of "required" field.

I'm open to closing the pull request, but considering the other one is also facing some open questions of backwards compatibility, I don't want to slow down this fix because of those unrelated topics.

camuthig avatar May 14 '24 15:05 camuthig

@camuthig So, what about this PR?

nstonic avatar Aug 29 '24 12:08 nstonic

@nstonic I'm not sure what the situation here is. @vitalik would you consider this pull request as an simpler version of the referenced #1139, that one is tackling a much deeper problem. This pull request has a smaller scope and only makes sure that the alias configurations are respected in the JSON schema generation. I think the other PR can still be considered as a progression of what this one accomplishes.

camuthig avatar Aug 29 '24 15:08 camuthig

This also fixes the problem of computed fields missing from the schema, as reported by issue #1292 and #960

tu-pm avatar Oct 02 '24 10:10 tu-pm

@vitalik any update on this?

oubeichen avatar Oct 22 '24 09:10 oubeichen