Skip to content

JSON Schema 2020-12: refract and dereference definitions like $defs #5303

Open

Description

@robert-hebel-sb

Is your feature request related to a problem?

The JSON Schema 2020-12 namespace does not refract definitions, so a $ref written inside a definitions entry is not dereferenced. The same schema under $defs is fully dereferenced.

The 2020-12 default meta-schema (https://json-schema.org/draft/2020-12/schema) still lists definitions (marked deprecated), and the validation specification, Appendix A, says: "implementations SHOULD assume that $defs and definitions have the same behavior when that meta-schema is used". Documents written for earlier drafts and declaring this $schema commonly use definitions.

Cause:

  • apidom-ns-json-schema-2019-09 removes the definitions fixed field (src/refractor/specification.ts, dissocPath(...)), and the definitions getter and setter of its JSONSchemaElement throw UnsupportedOperationError ("renamed to $defs").
  • apidom-ns-json-schema-2020-12 builds on it and doesn't re-add the field, so definitions is a generic member. Its entries are not JSON Schema elements and the dereference visitors don't visit them.

Reproduction (@swagger-api/apidom-ns-json-schema-2020-12 1.14.0):

const { refract } = require('@swagger-api/apidom-ns-json-schema-2020-12');

const e = refract({
  $schema: 'https://json-schema.org/draft/2020-12/schema',
  definitions: { A: { type: 'string' } },
  $defs: { B: { type: 'string' } },
});
for (const k of ['definitions', '$defs']) {
  const m = e.get(k);
  console.log(k, m.element, m.classes.toValue(), m.get(k === 'definitions' ? 'A' : 'B').element);
}
// definitions object [] object
// $defs object [ 'json-schema-$defs' ] JSONSchema202012

Through swagger-client 3.39.0 (json-schema-2020-12-apidom strategy), for this document:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "properties": { "product": { "$ref": "#/definitions/Product" } },
  "definitions": {
    "Product": { "type": "object", "properties": { "price": { "$ref": "#/definitions/Price" } } },
    "Price": { "type": "number", "exclusiveMinimum": 0 }
  }
}

properties.product is dereferenced including price, because the $ref points into definitions. But definitions.Product.properties.price stays { "$ref": "#/definitions/Price" }. With the entries under $defs instead, both are resolved.

Describe the solution you'd like

Refract definitions like $defs in the 2020-12 namespace: a fixed field reusing the $defs visitor, its class entry in refractor/plugins/replace-empty-element.ts, and a working getter and setter on JSONSchemaElement instead of the inherited throwing ones. Add refractor tests and an apidom-reference dereference test that resolves a $ref inside definitions.

The 2020-12 JSONSchemaElement is shared with the OpenAPI 3.1 and 3.2 and Arazzo namespaces, where definitions inside a Schema Object has no meaning. Those would have to keep it out (e.g. dissocPath in their specifications), or the behavior would have to be limited to standalone JSON Schema documents.

Describe alternatives you've considered

  • Resolving definitions outside ApiDOM (copying its entries under a temporary $defs before dereferencing, then moving them back) works but is a workaround that every consumer would have to repeat.
  • Leaving it as is and documenting that refs inside definitions are not resolved.

Additional context

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