Skip to content

🐛 Support custom OpenAPI / JSON Schema fields in the generated output OpenAPI#1429

Merged
tiangolo merged 4 commits intofastapi:masterfrom
jacobsvante:custom-schema-definition-fields
Jul 3, 2021
Merged

🐛 Support custom OpenAPI / JSON Schema fields in the generated output OpenAPI#1429
tiangolo merged 4 commits intofastapi:masterfrom
jacobsvante:custom-schema-definition-fields

Conversation

@jacobsvante
Copy link
Copy Markdown
Contributor

@jacobsvante jacobsvante commented May 18, 2020

Pydantic supports adding custom fields other than example and examples using BaseModel.Config.schema_extra. This change ensures that those extra fields exist in the FastAPI generated Open API schema as well.

This is in line with what the current docs say:

Other info
The same way, you could add your own custom extra info that would be added to the JSON Schema for each model, for example to customize a frontend user interface, etc.

(https://fastapi.tiangolo.com/tutorial/schema-extra-example/#other-info)

Pydantic supports adding custom fields other than `example` and `examples` using `BaseModel.Config.schema_extra`. This change ensures that those extra fields exist in the FastAPI generated Open API schema as well.

This is in line with what the current docs say under the section `Other Info` in the docs page `Schema Extra - Example`.
@codecov
Copy link
Copy Markdown

codecov bot commented May 18, 2020

Codecov Report

Merging #1429 (0868a37) into master (79f52c7) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1429   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          248       249    +1     
  Lines         7717      7740   +23     
=========================================
+ Hits          7717      7740   +23     
Impacted Files Coverage Δ
fastapi/openapi/models.py 100.00% <100.00%> (ø)
tests/test_custom_schema_fields.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 79f52c7...0868a37. Read the comment docs.

@tiangolo tiangolo changed the title Support custom OAS schema definition fields in components.schemas 🐛 Support custom OpenAPI / JSON Schema definition fields Jul 3, 2021
@tiangolo tiangolo changed the title 🐛 Support custom OpenAPI / JSON Schema definition fields 🐛 Support custom OpenAPI / JSON Schema fields in the generated output OpenAPI Jul 3, 2021
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jul 3, 2021

📝 Docs preview for commit 3c20f23 at: https://60e098eaae1c69f28f225bd6--fastapi.netlify.app

@tiangolo
Copy link
Copy Markdown
Member

tiangolo commented Jul 3, 2021

Good point, thanks @jmagnusson! Thanks for your contribution! ☕

And thanks for the patience 😅

@johnthagen
Copy link
Copy Markdown
Contributor

johnthagen commented Jul 29, 2021

Are custom fields supported by the OpenAPI spec?

I saw breakages using OpenAPI generator's schema validation when custom fields started appearing in the generated schema starting in FastAPI 0.66.0, which includes this PR.

A minimal reproducible example here: ormar-orm/ormar#291

@jacobsvante jacobsvante deleted the custom-schema-definition-fields branch July 29, 2021 17:04
@jacobsvante
Copy link
Copy Markdown
Contributor Author

Are custom fields supported by the OpenAPI spec?

Yes, see Specification Extensions here: https://swagger.io/specification/. All x- prefixed keys are allowed (though they mentioned that not all tooling supports it.)

@ml-evs
Copy link
Copy Markdown
Contributor

ml-evs commented Aug 27, 2021

I saw breakages using OpenAPI generator's schema validation when custom fields started appearing in the generated schema starting in FastAPI 0.66.0, which includes this PR.

We ran into this when just using some pydantic Field arguments (e.g. const) that now get erroneously added to the OpenAPI schema. I raised #3745 on this if it needs to be discussed; if I can get some consensus that it is a bug then I am happy to try and fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants