mansenfranzen / autodoc_pydantic

Seamlessly integrate pydantic models in your Sphinx documentation.
MIT License
159 stars 27 forks source link

@computed_field not showing in json schema #173

Open Galarzaa90 opened 1 year ago

Galarzaa90 commented 1 year ago

This is something I had also reported to FastAPI and it has been fixed.

The issue is that pydantic now has two serialization modes: input and serialization. Since a computed field's input doesn't need to be validated, it is not present in that mode (which seems to be the default).

This causes autodoc_pydantic to not show these fields in the Show JSON Schema section.

So it needs to be added:

model_json_schema(mode='serialization')
mansenfranzen commented 1 year ago

@Galarzaa90 Thanks for reporting this new feature in pydantic v2. This is indeed not supported in autodoc_pydantic yet. Does it make sense to allow the JSON output to be configurable to either choose the input or serialization mode? Or should we aim to simply use the serialization mode as default because it contains the model in its entirety. What is your opinion given your use case?

Galarzaa90 commented 1 year ago

@Galarzaa90 Thanks for reporting this new feature in pydantic v2. This is indeed not supported in autodoc_pydantic yet. Does it make sense to allow the JSON output to be configurable to either choose the input or serialization mode? Or should we aim to simply use the serialization mode as default because it contains the model in its entirety. What is your opinion given your use case?

I was thinking about the same thing, that is a tough question. I believe that serialization is more suitable for general use. But making it configurable using a directive is not a bad idea. But I believe the default should be serialization.

For example, a calculated's property schema (in serialization mode) is marked with the readOnly attribute, indicating that it is not possible to write to it, possibly communicating that it is not something you would use for input.

"isBattleyeProtected": {
    "description": "Whether the server is currently protected with BattlEye or not.",
    "readOnly": true,
    "type": "boolean"
}

On the other hand, the property would simply be omitted in input mode.