New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Syntactic tweaks to models and schemas for compatibility with fastapi>0.66
#1131
Conversation
5cf97c1
to
09fdad2
Compare
Thanks for the review @JPBergsma, I've put this PR on hold as I think this is going to more complicated than I first thought. |
Concerning |
Ah, I see this was already the case. Never mind :) |
Just FYI, I got a response on my FastAPI issue, it seems like there will not be a fix. They accepted my PR adding a warning to the docs: https://fastapi.tiangolo.com/tutorial/body-fields/#add-extra-information |
Thinking about this more, we might just have to bite the bullet and change how we test the OpenAPI schema. It looks like MP can't update to the latest OPTIMADE version atm as they are already using a more recent FastAPI. I'll see what I can do in time for the v0.18 release. |
2160d04
to
82572cc
Compare
Codecov Report
@@ Coverage Diff @@
## master #1131 +/- ##
==========================================
- Coverage 91.95% 91.84% -0.12%
==========================================
Files 72 72
Lines 4265 4279 +14
==========================================
+ Hits 3922 3930 +8
- Misses 343 349 +6
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
fastapi>0.66
"x-optimade-unit", | ||
"x-optimade-queryable", | ||
"x-optimade-support", | ||
"x-optimade-unit", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have listed "x-optimade-unit" unit twice here. That seems a bit strange.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm yes, this is a typo, though not a consequential one. I will remember to fix it in the next PR I make
As mentioned in PR #1131 "x-optimade-unit" occurs twice by mistake.
As mentioned in PR #1131 "x-optimade-unit" occurs twice by mistake.
As mentioned in PR #1131 "x-optimade-unit" occurs twice by mistake.
This PR makes 2 main changes to ensure FastAPI compatibility going forward (see #887).
The issue is essentially that FastAPI broke OpenAPI specs generated from pydantic by adding
extra = 'allow'
to its base OpenAPI schema class (see tiangolo/fastapi#3745). For example, if you usedField(const=True)
in pydantic, theconst
key would be added to the eventual OpenAPI schema, yetconst
does not exist as an OpenAPI field. The same then happened for our custom schema injections ofsupport
,queryable
andunit
.This PR adds the following workarounds:
const
andpattern
with just the pydanticregex
(which checks the value with the regex and also injectspattern
into the schema).typing.Literal
, replacing constant values in the OpenAPI schema with single-valued enums. The other option here would be to remove theconst
check and replacepattern
withregex
so that pydantic applies the regex during validation.support
becomes e.g.x-optimade-support
. These need to be retained in the JSONSchema so that they can be used to set the OpenAPI nullable flag.OptimadeField
already.