-
Notifications
You must be signed in to change notification settings - Fork 44
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
Describe query parameters in OpenAPI schema #166
Describe query parameters in OpenAPI schema #166
Conversation
Maybe we should remove |
This looks great @CasperWA, if you write the constraints I'm happy to write the tests so it can be thoroughly checked! |
Remove PEP8 E251/E252 (unexpected spaces around keyword / parameter equals) from lint warnings. Update `page_page` to `page_number` according to Materials-Consortia/OPTIMADE#187. Due to pydantic: https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints the `ge` parameter cannot be used together with a non-standard type, e.g. NonnegativeInt. Instead the OpenAPI parameter is set directly, here `minimum`. Regular expressions are added for `response_fields` and `sort` based on the regular expressions provided for an `identifier` in the OPTiMaDe spec.
7f1d660
to
5770c8d
Compare
I have now removed |
The latest commit upgrades Concerning the two other uses of |
Remove unused `_` in validator
Using `IntEnum` and pydantic's `Field` parameters to perform validation of the property, `dimension_types` is now represented correctly in the OpenAPI schema as well.
137edee
to
2f41f09
Compare
Can we switch the enum names to PERIODIC and APERIODIC or something that is more descriptive? This provides a way of documenting the value in the code itself. |
Remove and replace `conlist` with List of Tuples containing the exact number of components needed, i.e., List[Tuple[Union[float, None]]] means the value MUST be [ [ null ], [ 1.0 ], [ 0 ], ... ]. The internal "lists" MUST be of length 1, if more types are added in Tuple _that_ is the exact allowed length of the internal "lists". For `lattice_vectors` the outer list has also been constrained via pydantic's `Field` using `min_items` and `max_items`. Some "bad" and "good" test structures have been added to be tested in `test_models.py`.
Yup, was the next thing I was going to do. I like those names much better - this was merely to check if it worked to begin with. It seems I have gotten rid of |
Question: why are the components of lattice vectors are optional None? Vector3D = Tuple[float,float,float] rather than what is effectively: Vector3D = Tuple[Optional[float],Optional[float],Optional[float]] The final recommendation if anyone wants to do this is to validate fractional coordinates with something like this: import numpy as np
from pydantic import BaseModel, validator
class FractionalVector3D(BaseModel):
__root__ : Tuple[float,float,float]
@validator("__root__")
def check_fractional(cls, vector):
vector = np.mod(vector,1.0).tolist()
return vector |
It is in the spec, see here.
I deliberately didn't use
Nice. Although we were trying not to get |
We also don't have fractional coordinates in yet, see the discussion in Materials-Consortia/OPTiMaDe/#206. |
True, but that PR still has some way to go I think. |
I can recreate the fractional class pretty easily and you can search in github PRs so saving it shouldn't be a problem. |
Sure, it was also to have a place to discuss it and its implementation after this PR has been closed. But fine. |
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.
I mostly recommend the Tuple over the List whenever you know the exact size of the data because it shows up in the JSON schema as a little clearer. For instance, when I use Tuple[Vector3D, Vector3D, Vector3D]
i get an JSON schema that looks like this:
Vector3D = Tuple[Union[float,None], Union[float,None], Union[float,None]]
class Matrix(BaseModel):
__root__: Tuple[Vector3D,Vector3D,Vector3D]
print(Matrix.schema())
{'title': 'Matrix',
'type': 'array',
'items': [{'type': 'array',
'items': [{'type': 'number'}, {'type': 'number'}, {'type': 'number'}]},
{'type': 'array',
'items': [{'type': 'number'}, {'type': 'number'}, {'type': 'number'}]},
{'type': 'array',
'items': [{'type': 'number'}, {'type': 'number'}, {'type': 'number'}]}]}
This is more verbose but it shows appropriately as a 3x3 array as opposed to an array constrained to size 3 of array of 3 floats.
Fair enough. I think it becomes quite verbose, but it may be clearer on the whole - especially when compared with |
Co-authored-by: Shyam Dwaraknath <[email protected]>
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.
This looks good for what this PR is supposed to do. We can discuss the float/None validation elsewhere before making a PR for that since it doesn't affect the OpenAPI schema.
Bump to v0.4.0 Changes: Changes: - Reorder test files and update testing endpoints setup (#162, @CasperWA) - Separate the regular and index-meta database server, making sure they're not importing each other (#160, @CasperWA) - Introduce Starlette/FastAPI HTTP middleware for redirecting URLs ending with a slash to URLs _not_ ending with a slash (#160, @CasperWA) - Fix validation of `dimension_types` resulting in `response_fields` failing and add tests for `response_fields` (#153, @CasperWA) - Update entry list property descriptions according to updated OPTiMaDe spec (#153, @CasperWA) - Validate updated entry list properties and test updated entry list properties (#153, @ml-evs) - Add OpenAPI schema descriptions for query parameters (#166, @CasperWA) - Remove custom constrained types `NonnegativeInt` and `conlist` and use instead standard types together with `pydantic`'s `FieldInfo` parameters for schema generation and validation (#166, @shyamd, @CasperWA)
Closes #164
Fixes #165
Fixes #55
Remove PEP8 E251/E252 (unexpected spaces around keyword / parameter equals) from lint warnings.
Update
page_page
topage_number
according to Materials-Consortia/OPTIMADE#187.Due to pydantic: https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints the
ge
parameter cannot be used together with a non-standard type, e.g. NonnegativeInt. Instead the OpenAPI parameter is set directly, hereminimum
.Regular expressions are added for
response_fields
andsort
based on the regular expressions provided for anidentifier
in the OPTiMaDe spec.This PR supersedes #56 by using
Tuple
s inList
s for the lists in list values (cartesian_site_positions
andlattice_vectors
). Furthermore, anIntEnum
is used to constraindimension_types
to only use 0 and 1s. The customconlist
has now been completely removed.Most importantly, the constraints are reflected in the OpenAPI schema.