feat(samples): support new semantics of exclusive number ranges (#8885)

This change is specific to JSON Schema 2020-12
and OpenAPI 3.1.0.

Refs #8577
This commit is contained in:
Vladimír Gorej
2023-06-06 13:49:02 +02:00
committed by GitHub
parent bdad2fe83d
commit 836659d8ac
2 changed files with 18 additions and 19 deletions

View File

@@ -694,20 +694,22 @@ export const sampleFromSchemaGeneric = (
// display schema default // display schema default
value = primitive(schema) value = primitive(schema)
if (typeof value === "number") { if (typeof value === "number") {
let min = schema.minimum let minValue = null
if (min !== undefined && min !== null) { let maxValue = null
if (schema.exclusiveMinimum) { const { minimum, maximum, exclusiveMinimum, exclusiveMaximum } = schema
min++ if (typeof minimum === "number") {
} minValue = minimum
value = min
} }
let max = schema.maximum if (typeof exclusiveMinimum === "number" && exclusiveMinimum > minValue) {
if (max !== undefined && max !== null) { minValue = exclusiveMinimum + 1
if (schema.exclusiveMaximum) {
max--
}
value = max
} }
if (typeof maximum === "number") {
maxValue = maximum
}
if (typeof exclusiveMaximum === "number" && exclusiveMaximum < maxValue) {
maxValue = exclusiveMaximum - 1
}
value = minValue || maxValue || value
} }
if (typeof value === "string") { if (typeof value === "string") {
if (schema.maxLength !== null && schema.maxLength !== undefined) { if (schema.maxLength !== null && schema.maxLength !== undefined) {

View File

@@ -1298,13 +1298,11 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected) expect(sampleFromSchema(definition)).toEqual(expected)
}) })
it("should handle minimum with exclusive", () => { it("should handle exclusiveMinimum", () => {
const definition = { const definition = {
type: "number", type: "number",
minimum: 5, exclusiveMinimum: 5,
exclusiveMinimum: true,
} }
const expected = 6 const expected = 6
expect(sampleFromSchema(definition)).toEqual(expected) expect(sampleFromSchema(definition)).toEqual(expected)
@@ -1321,11 +1319,10 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected) expect(sampleFromSchema(definition)).toEqual(expected)
}) })
it("should handle maximum with exclusive", () => { it("should handle exclusiveMaximum", () => {
const definition = { const definition = {
type: "number", type: "number",
maximum: -1, exclusiveMaximum: -1,
exclusiveMaximum: true,
} }
const expected = -2 const expected = -2