feat(samples): add support for const keyword (#8884)
This change is specific to JSON Schema 2020-12 and OpenAPI 3.1.0. Refs #8577
This commit is contained in:
@@ -101,6 +101,7 @@ const liftSampleHelper = (oldSchema, target, config = {}) => {
|
|||||||
"enum",
|
"enum",
|
||||||
"xml",
|
"xml",
|
||||||
"type",
|
"type",
|
||||||
|
"const",
|
||||||
...objectContracts,
|
...objectContracts,
|
||||||
...arrayContracts,
|
...arrayContracts,
|
||||||
...numberContracts,
|
...numberContracts,
|
||||||
@@ -683,7 +684,10 @@ export const sampleFromSchemaGeneric = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
let value
|
let value
|
||||||
if (schema && Array.isArray(schema.enum)) {
|
if (typeof schema?.const !== "undefined") {
|
||||||
|
// display const value
|
||||||
|
value = schema.const
|
||||||
|
} else if (schema && Array.isArray(schema.enum)) {
|
||||||
//display enum first value
|
//display enum first value
|
||||||
value = normalizeArray(schema.enum)[0]
|
value = normalizeArray(schema.enum)[0]
|
||||||
} else if (schema) {
|
} else if (schema) {
|
||||||
|
|||||||
@@ -86,32 +86,33 @@ describe("sampleFromSchema", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should handle type keyword defined as list of types", function () {
|
it("should handle type keyword defined as list of types", function () {
|
||||||
const definition = fromJS({
|
const definition = fromJS({ type: ["object", "string"] })
|
||||||
type: ["object", "string"],
|
|
||||||
})
|
|
||||||
const expected = {}
|
const expected = {}
|
||||||
|
|
||||||
expect(sampleFromSchema(definition)).toEqual(expected)
|
expect(sampleFromSchema(definition)).toEqual(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should prioritize array when array and object defined as list of types", function () {
|
it("should prioritize array when array and object defined as list of types", function () {
|
||||||
const definition = fromJS({
|
const definition = fromJS({ type: ["object", "array"] })
|
||||||
type: ["object", "array"],
|
|
||||||
})
|
|
||||||
const expected = []
|
const expected = []
|
||||||
|
|
||||||
expect(sampleFromSchema(definition)).toEqual(expected)
|
expect(sampleFromSchema(definition)).toEqual(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should handle primitive types defined as list of types", function () {
|
it("should handle primitive types defined as list of types", function () {
|
||||||
const definition = fromJS({
|
const definition = fromJS({ type: ["string", "number"] })
|
||||||
type: ["string", "number"],
|
|
||||||
})
|
|
||||||
const expected = "string"
|
const expected = "string"
|
||||||
|
|
||||||
expect(sampleFromSchema(definition)).toEqual(expected)
|
expect(sampleFromSchema(definition)).toEqual(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should return const value", function () {
|
||||||
|
const definition = fromJS({ const: 3 })
|
||||||
|
const expected = 3
|
||||||
|
|
||||||
|
expect(sampleFromSchema(definition)).toStrictEqual(expected)
|
||||||
|
})
|
||||||
|
|
||||||
it("handles Immutable.js objects for nested schemas", function () {
|
it("handles Immutable.js objects for nested schemas", function () {
|
||||||
const definition = fromJS({
|
const definition = fromJS({
|
||||||
type: "object",
|
type: "object",
|
||||||
|
|||||||
Reference in New Issue
Block a user