The InnerShadowEffect does not have type listed as a required property, which can create a problem when you run this schema through the datamodel-code-generator tool. Documentation for that tool can be found here - https://datamodel-code-generator.koxudaxi.dev/.
generated code without type set as required:
class InnerShadowEffect(BaseShadowEffect):
type: Literal["INNER_SHADOW"] | None = Field(
None,
description="A string literal representing the effect's type. Always check the type before reading other properties.",
)
generated code with type set as required:
class InnerShadowEffect(BaseShadowEffect):
type: Literal["INNER_SHADOW"] = Field(
...,
description="A string literal representing the effect's type. Always check the type before reading other properties.",
)
The key differences - no Union operator with None (| None), and the default is ... instead of None.
The code without the fix cannot be imported - it produces the following error:
pydantic.errors.PydanticUserError: Model 'InnerShadowEffect' needs field 'type' to be of type `Literal`
With the fix, the import works fine.
type should be required, because it is used as the discriminator here:
discriminator:
propertyName: type
mapping:
DROP_SHADOW: "#/components/schemas/DropShadowEffect"
INNER_SHADOW: "#/components/schemas/InnerShadowEffect"
LAYER_BLUR: "#/components/schemas/BlurEffect"
BACKGROUND_BLUR: "#/components/schemas/BlurEffect"
TEXTURE: "#/components/schemas/TextureEffect"
NOISE: "#/components/schemas/NoiseEffect"
It appears all other items in that mapping list DO have type set as required.
Reproducing the Error
Here is a pyproject.toml for running the generator:
[tool.datamodel-codegen]
url = "https://github.com/figma/rest-api-spec/raw/refs/tags/v0.35.0/openapi/openapi.yaml"
input-file-type = "openapi"
output = "models.py"
output-model-type = "pydantic_v2.BaseModel"
formatters = ["isort", "ruff-format"]
enum-field-as-literal = "all"
Note that the enum-field-as-literal flag set to all.
With the above, run datamodel-codegen, then just run import models in a python REPL to see if an error occurs or not.
The
InnerShadowEffectdoes not havetypelisted as arequiredproperty, which can create a problem when you run this schema through thedatamodel-code-generatortool. Documentation for that tool can be found here - https://datamodel-code-generator.koxudaxi.dev/.generated code without
typeset asrequired:generated code with
typeset asrequired:The key differences - no Union operator with None (
| None), and the default is...instead ofNone.The code without the fix cannot be imported - it produces the following error:
With the fix, the
importworks fine.typeshould be required, because it is used as thediscriminatorhere:It appears all other items in that mapping list DO have
typeset asrequired.Reproducing the Error
Here is a
pyproject.tomlfor running the generator:Note that the
enum-field-as-literalflag set toall.With the above, run
datamodel-codegen, then just runimport modelsin a python REPL to see if an error occurs or not.