Skip to content

Preserve OpenAPI 3.1 schema $ref siblings - #1271

Open
abramchikd wants to merge 3 commits into
getkin:masterfrom
abramchikd:fix/internalize-oas30-sibling-refs
Open

abramchikd wants to merge 3 commits into
getkin:masterfrom
abramchikd:fix/internalize-oas30-sibling-refs

Conversation

@abramchikd

Copy link
Copy Markdown

Summary

OpenAPI 3.1 treats Schema Object keywords next to $ref as additional constraints. Previously, kin-openapi decoded these fields but dropped them during serialization and did not apply them consistently after reference resolution.

This change preserves schema $ref siblings across loading, validation, serialization, and reference internalization while keeping the existing SchemaRef.Value API and OpenAPI 3.0 resolution behavior.

Changes

  • Decode sibling keywords through the ordinary Schema representation so they follow the same JSON/YAML round-trip rules as schemas without $ref.
  • Resolve OpenAPI 3.1 siblings conjunctively by keeping the local schema as the effective value and adding the resolved target as an allOf conjunct.
  • Serialize from the local sibling schema while retaining $ref as the reference boundary, avoiding target duplication and recursive marshaling.
  • Handle self-references, longer cycles, boolean schemas, dynamic references, raw external schemas, and repeated resolution without losing constraints or mutating shared targets.
  • Internalize references nested inside sibling schemas for both OpenAPI 3.0 and 3.1 documents without changing OpenAPI 3.0 validation semantics.
  • Preserve use-site extensions and origin information.
  • Centralize schema-child traversal so resolution and internalization cover every subschema field consistently.
  • Make recursive instance tracking distinguish overlapping slices that share a backing address.
  • Regenerate the reference types from the updated templates.

Compatibility

  • SchemaRef.Value remains a non-nil *Schema after successful resolution.
  • OpenAPI 3.0 continues to validate against the resolved target only.
  • Serialization remains version-independent and now retains sibling fields supported by ordinary Schema serialization.

Testing

Added regression coverage for:

  • internal and external references with sibling constraints;
  • JSON and YAML serialization parity, including omitted values and pointer-backed zero values;
  • conjunctive validation when sibling and target constraints overlap;
  • self-references, multi-node cycles, boolean schemas, and $dynamicRef;
  • idempotent resolution and isolation between references sharing one target;
  • external-reference internalization, including nested references in sibling schemas and OpenAPI 3.0 compatibility;
  • origin preservation and recursive validation of overlapping slices.

This pull request was created with the assistance of OpenAI Codex.

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