Skip to content

DateTime(format="timestamp") generates invalid OpenAPI schema using min instead of minimum #1064

Description

@ULisichkin

Description

apispec generates an invalid OpenAPI schema for marshmallow.fields.DateTime(format="timestamp").

The generated schema contains the min property:

changedOn:
  type: number
  format: float
  example: "1676451245.596"
  min: "0"

However, min is not a valid OpenAPI Schema Object property. As a result, the generated specification fails validation with openapi-spec-validator:

OpenAPIValidationError:
Property 'min' is not allowed

This was reproduced with:

  • apispec 6.10.0
  • marshmallow 3.26.2
  • openapi-spec-validator 0.9.0
  • OpenAPI 3.0.2

Expected behavior

The generated schema should use the standard OpenAPI keyword minimum instead of min, for example:

changedOn:
  type: number
  format: float
  example: "1676451245.596"
  minimum: 0

or omit the constraint entirely if no minimum value is intended.

Minimal reproduction

from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from marshmallow import Schema, fields
from pprint import pprint

class TestSchema(Schema):
    ts = fields.DateTime(format="timestamp")

spec = APISpec(
    title="test",
    version="1.0",
    openapi_version="3.0.2",
    plugins=[MarshmallowPlugin()],
)

spec.components.schema("Test", schema=TestSchema)

pprint(spec.to_dict()["components"]["schemas"]["Test"])

Output:

{'additionalProperties': False,
 'properties': {'ts': {'example': '1676451245.596',
                       'format': 'float',
                       'min': '0',
                       'type': 'number'}},
 'type': 'object'}

The file field_converter.py contains the code that causes this issue:

            elif field.format == "timestamp":
                ret = {
                    "type": "number",
                    "format": "float",
                    "example": "1676451245.596",
                    "min": "0",
                }
            elif field.format == "timestamp_ms":
                ret = {
                    "type": "number",
                    "format": "float",
                    "example": "1676451277514.654",
                    "min": "0",
                }

Specification reference

The OpenAPI Schema Object defines the numeric constraint keyword as minimum; min is not a valid keyword.

OpenAPI 3.0.2 Schema Object:
https://spec.openapis.org/oas/v3.0.2#schema-object

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions