refactor(samples): type check object constraints acording to spec (#8898)
This change is specific to JSON Schema 2020-12 and OpenAPI 3.1.0. Refs #8577
This commit is contained in:
@@ -122,7 +122,7 @@ const applyArrayConstraints = (array, constraints = {}) => {
|
|||||||
const sanitizeRef = (value) =>
|
const sanitizeRef = (value) =>
|
||||||
deeplyStripKey(value, "$$ref", (val) => typeof val === "string" && isURI(val))
|
deeplyStripKey(value, "$$ref", (val) => typeof val === "string" && isURI(val))
|
||||||
|
|
||||||
const objectContracts = ["maxProperties", "minProperties"]
|
const objectConstraints = ["maxProperties", "minProperties", "required"]
|
||||||
const arrayConstraints = [
|
const arrayConstraints = [
|
||||||
"minItems",
|
"minItems",
|
||||||
"maxItems",
|
"maxItems",
|
||||||
@@ -153,7 +153,7 @@ const liftSampleHelper = (oldSchema, target, config = {}) => {
|
|||||||
"xml",
|
"xml",
|
||||||
"type",
|
"type",
|
||||||
"const",
|
"const",
|
||||||
...objectContracts,
|
...objectConstraints,
|
||||||
...arrayConstraints,
|
...arrayConstraints,
|
||||||
...numberConstraints,
|
...numberConstraints,
|
||||||
...stringConstraints,
|
...stringConstraints,
|
||||||
@@ -325,7 +325,7 @@ export const sampleFromSchemaGeneric = (
|
|||||||
const schemaHasAny = (keys) => keys.some((key) => Object.hasOwn(schema, key))
|
const schemaHasAny = (keys) => keys.some((key) => Object.hasOwn(schema, key))
|
||||||
// try recover missing type
|
// try recover missing type
|
||||||
if (schema && typeof type !== "string" && !Array.isArray(type)) {
|
if (schema && typeof type !== "string" && !Array.isArray(type)) {
|
||||||
if (properties || additionalProperties || schemaHasAny(objectContracts)) {
|
if (properties || additionalProperties || schemaHasAny(objectConstraints)) {
|
||||||
type = "object"
|
type = "object"
|
||||||
} else if (items || contains || schemaHasAny(arrayConstraints)) {
|
} else if (items || contains || schemaHasAny(arrayConstraints)) {
|
||||||
type = "array"
|
type = "array"
|
||||||
@@ -359,8 +359,8 @@ export const sampleFromSchemaGeneric = (
|
|||||||
|
|
||||||
const hasExceededMaxProperties = () =>
|
const hasExceededMaxProperties = () =>
|
||||||
schema &&
|
schema &&
|
||||||
schema.maxProperties !== null &&
|
Number.isInteger(schema.maxProperties) &&
|
||||||
schema.maxProperties !== undefined &&
|
schema.maxProperties > 0 &&
|
||||||
propertyAddedCounter >= schema.maxProperties
|
propertyAddedCounter >= schema.maxProperties
|
||||||
|
|
||||||
const requiredPropertiesToAdd = () => {
|
const requiredPropertiesToAdd = () => {
|
||||||
@@ -392,11 +392,7 @@ export const sampleFromSchemaGeneric = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const canAddProperty = (propName) => {
|
const canAddProperty = (propName) => {
|
||||||
if (
|
if (!schema || schema.maxProperties == null) {
|
||||||
!schema ||
|
|
||||||
schema.maxProperties === null ||
|
|
||||||
schema.maxProperties === undefined
|
|
||||||
) {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if (hasExceededMaxProperties()) {
|
if (hasExceededMaxProperties()) {
|
||||||
@@ -759,8 +755,8 @@ export const sampleFromSchemaGeneric = (
|
|||||||
res[displayName].push(additionalPropSample)
|
res[displayName].push(additionalPropSample)
|
||||||
} else {
|
} else {
|
||||||
const toGenerateCount =
|
const toGenerateCount =
|
||||||
schema.minProperties !== null &&
|
Number.isInteger(schema.minProperties) &&
|
||||||
schema.minProperties !== undefined &&
|
schema.minProperties > 0 &&
|
||||||
propertyAddedCounter < schema.minProperties
|
propertyAddedCounter < schema.minProperties
|
||||||
? schema.minProperties - propertyAddedCounter
|
? schema.minProperties - propertyAddedCounter
|
||||||
: 3
|
: 3
|
||||||
|
|||||||
Reference in New Issue
Block a user