Skip to content

fix(jsonapi): correct relationship schemas in generated json schema#8321

Merged
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/jsonapi-jsonschema-relation-bugs
Jun 17, 2026
Merged

fix(jsonapi): correct relationship schemas in generated json schema#8321
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/jsonapi-jsonschema-relation-bugs

Conversation

@soyuka

@soyuka soyuka commented Jun 17, 2026

Copy link
Copy Markdown
Member
Q A
Branch? 4.3
Tickets
License MIT

Problem

Two pre-existing defects in the JSON:API JSON Schema generation, surfaced while reviewing #8313 (both live in buildDefinitionPropertiesSchema, untouched by that PR):

  1. Relationship linkage is under-specified. The resource identifier object reused for every relationship data (RELATION_PROPS) declares type and id but marks neither required. The JSON:API spec says a resource identifier object MUST contain both — see Resource Identifier Objects.

  2. Polymorphic relations lose targets in included. $relatedDefinitions[$propertyName] = array_flip($refs) collapses the ref map to a single entry because every value is the literal string '$ref'array_flip keeps only the last key. A relation typed as a union of several resource classes therefore advertises only one of them under included.anyOf; the others vanish.

Fix

  • Add 'required' => ['type', 'id'] to RELATION_PROPS.
  • Build $relatedDefinitions as a flat map keyed by ref URI, so every target of a polymorphic relation is listed in included.anyOf. This also dedupes refs shared across multiple relationships.

Tests

  • testRelationshipLinkageRequiresTypeAndId: to-one linkage requires type + id.
  • testIncludedListsAllPolymorphicRelationTargets: a union-typed relation lists every target in included.anyOf (new OtherRelatedDummy fixture).

Single-class included behaviour is unchanged (existing functional tests still green).

- relationship linkage is a resource identifier object: require type
  and id, per https://jsonapi.org/format/#document-resource-identifier-objects
- list every target of a polymorphic (union-typed) relation in
  included.anyOf instead of collapsing to the last via array_flip,
  which also dedupes refs shared across relationships
@soyuka soyuka force-pushed the fix/jsonapi-jsonschema-relation-bugs branch from 80bb21f to 0fe94ff Compare June 17, 2026 10:13
@soyuka soyuka merged commit 8999b60 into api-platform:4.3 Jun 17, 2026
110 of 112 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant