apiaryio / drafter

API Blueprint Parser (C++)
https://apiblueprint.org/
MIT License
301 stars 54 forks source link

Optimise JSON Schemas generated for fixed-type arrays #789

Closed kylef closed 3 years ago

kylef commented 3 years ago

This changeset optimises the generation of JSON Schema of fixed-type array's with a single element inside them to be placed directly inside "items", instead of inside an "anyOf" inside "items". A user was asking about this behaviour in https://github.com/apiaryio/api-blueprint/issues/450.

In terms of JSON Schema validation these behave the same, the new behaviour produces a smaller, clearer JSON Schema.

While doing so, I noticed that Drafter would produce an invalid schema in the case where there is an empty fixed-type array. Drafter would emit a sub-schema such as {"anyOf": []} which breaks the clause in JSON Schema 2019-09 Section 9.2.1.2:

This keyword's value MUST be a non-empty array.

Instead Drafter will now emit a schema with maxItems set to 0 to indicate the array must be empty. This matches the intended behaviour from Drafter test specification:

Given: An empty ArrayElement with fixedType attribute true When: a JSON Schema is generated from it Then: the schema matches an empty array

These two changes are separated into different commits thus I recommend reviewing per-commit.

Closes https://github.com/apiaryio/api-blueprint/issues/450 Closes https://github.com/apiaryio/drafter/issues/694