Skip to content

Commit

Permalink
Remove nullable field from properties
Browse files Browse the repository at this point in the history
This is no longer part of the OpenAPI spec as of v3.1.

Fixes #1814
  • Loading branch information
CasperWA authored and ml-evs committed Oct 15, 2023
1 parent 78373f0 commit d50524c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 32 deletions.
32 changes: 1 addition & 31 deletions optimade/models/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import re
import warnings
from enum import Enum, IntEnum
from typing import Annotated, Any, Optional, Union
from typing import Annotated, Optional, Union

from pydantic import (
BaseModel,
BeforeValidator,
ConfigDict,
Field,
ValidationInfo,
field_validator,
Expand Down Expand Up @@ -272,38 +271,9 @@ def check_self_consistency(
)


def structure_json_schema_extra(
schema: dict[str, Any], model: type["StructureResourceAttributes"]
) -> None:
"""Two things need to be added to the schema:
1. Constrained types in pydantic do not currently play nicely with
"Required Optional" fields, i.e. fields must be specified but can be null.
The two constrained list fields, `dimension_types` and `lattice_vectors`,
are OPTIMADE 'SHOULD' fields, which means that they are allowed to be null.
2. All OPTIMADE 'SHOULD' fields are allowed to be null, so we manually set them
to be `nullable` according to the OpenAPI definition.
"""
schema["required"].insert(7, "dimension_types")
schema["required"].insert(9, "lattice_vectors")

nullable_props = (
prop
for prop in schema["required"]
if schema["properties"][prop].get("x-optimade-support")
== SupportLevel.SHOULD.value
)
for prop in nullable_props:
schema["properties"][prop]["nullable"] = True


class StructureResourceAttributes(EntryResourceAttributes):
"""This class contains the Field for the attributes used to represent a structure, e.g. unit cell, atoms, positions."""

model_config = ConfigDict(json_schema_extra=structure_json_schema_extra)

elements: Optional[list[str]] = OptimadeField(
None,
description="""The chemical symbols of the different elements present in the structure.
Expand Down
1 change: 0 additions & 1 deletion optimade/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def StrictField(
allowed_keys = [
"pattern",
"uniqueItems",
"nullable",
] + OPTIMADE_SCHEMA_EXTENSION_KEYS
_banned = [k for k in kwargs if k not in set(_PYDANTIC_FIELD_KWARGS + allowed_keys)]

Expand Down

0 comments on commit d50524c

Please sign in to comment.