Skip to content

Commit

Permalink
refactor(samples): type check object constraints acording to spec (#8898
Browse files Browse the repository at this point in the history
)

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

Refs #8577
  • Loading branch information
char0n committed Jun 8, 2023
1 parent 521a40a commit 220150e
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/core/plugins/json-schema-2020-12/samples-extensions/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const applyArrayConstraints = (array, constraints = {}) => {
const sanitizeRef = (value) =>
deeplyStripKey(value, "$$ref", (val) => typeof val === "string" && isURI(val))

const objectContracts = ["maxProperties", "minProperties"]
const objectConstraints = ["maxProperties", "minProperties", "required"]
const arrayConstraints = [
"minItems",
"maxItems",
Expand Down Expand Up @@ -153,7 +153,7 @@ const liftSampleHelper = (oldSchema, target, config = {}) => {
"xml",
"type",
"const",
...objectContracts,
...objectConstraints,
...arrayConstraints,
...numberConstraints,
...stringConstraints,
Expand Down Expand Up @@ -325,7 +325,7 @@ export const sampleFromSchemaGeneric = (
const schemaHasAny = (keys) => keys.some((key) => Object.hasOwn(schema, key))
// try recover missing type
if (schema && typeof type !== "string" && !Array.isArray(type)) {
if (properties || additionalProperties || schemaHasAny(objectContracts)) {
if (properties || additionalProperties || schemaHasAny(objectConstraints)) {
type = "object"
} else if (items || contains || schemaHasAny(arrayConstraints)) {
type = "array"
Expand Down Expand Up @@ -359,8 +359,8 @@ export const sampleFromSchemaGeneric = (

const hasExceededMaxProperties = () =>
schema &&
schema.maxProperties !== null &&
schema.maxProperties !== undefined &&
Number.isInteger(schema.maxProperties) &&
schema.maxProperties > 0 &&
propertyAddedCounter >= schema.maxProperties

const requiredPropertiesToAdd = () => {
Expand Down Expand Up @@ -392,11 +392,7 @@ export const sampleFromSchemaGeneric = (
}

const canAddProperty = (propName) => {
if (
!schema ||
schema.maxProperties === null ||
schema.maxProperties === undefined
) {
if (!schema || schema.maxProperties == null) {
return true
}
if (hasExceededMaxProperties()) {
Expand Down Expand Up @@ -759,8 +755,8 @@ export const sampleFromSchemaGeneric = (
res[displayName].push(additionalPropSample)
} else {
const toGenerateCount =
schema.minProperties !== null &&
schema.minProperties !== undefined &&
Number.isInteger(schema.minProperties) &&
schema.minProperties > 0 &&
propertyAddedCounter < schema.minProperties
? schema.minProperties - propertyAddedCounter
: 3
Expand Down

0 comments on commit 220150e

Please sign in to comment.