Skip to content

json schema: propertyNames only shows up under specific conditions #866

@bimac

Description

@bimac

Description

I found a weird inconsistency when generating JSON schema:

Variant 1

import msgspec
from typing import Annotated
import json

key_type = Annotated[str, msgspec.Meta(pattern=r'^A$')]
value_type = Annotated[str, msgspec.Meta(pattern=r'^B$')]
dict_type = Annotated[dict[key_type, value_type], msgspec.Meta(min_length=1)]

schema = msgspec.json.schema(dict_type)
print(json.dumps(schema, indent=2))

results in:

{
  "type": "object",
  "propertyNames": {
    "pattern": "^A$"
  },
  "additionalProperties": {
    "type": "string",
    "pattern": "^B$"
  },
  "minProperties": 1
}

✔️ works as expected

Variant 2

import msgspec
from typing import Annotated
import json

key_type = Annotated[str, msgspec.Meta(title='key', pattern=r'^A$')]
value_type = Annotated[str, msgspec.Meta(pattern=r'^B$')]
dict_type = Annotated[dict[key_type, value_type], msgspec.Meta(min_length=1)]

schema = msgspec.json.schema(dict_type)
print(json.dumps(schema, indent=2))

results in:

{
  "type": "object",
  "additionalProperties": {
    "type": "string",
    "pattern": "^B$"
  },
  "minProperties": 1
}

❌ Merely adding the title kwarg to key_type results in the propertyNames section being dropped entirely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions