Skip to content

Commit

Permalink
[Bugfix] Fix complex intersections of allOf/anyOf/properties/required (
Browse files Browse the repository at this point in the history
…fix #381)
  • Loading branch information
bcherny committed Jan 21, 2024
1 parent 6adcad9 commit b3d8d5f
Show file tree
Hide file tree
Showing 8 changed files with 4,854 additions and 508 deletions.
14 changes: 14 additions & 0 deletions src/normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ rules.set('Transform const to singleton enum', schema => {
}
})

// @see https://json-schema.org/understanding-json-schema/reference/combining#factoring-schemas
rules.set('Propagate additionalProperties=false to all factors', schema => {
const parent = schema[Parent]
if (!parent || parent.additionalProperties !== false) {
return
}

if (schema.hasOwnProperty('additionalProperties')) {
return
}

schema.additionalProperties = false
})

export function normalize(
rootSchema: LinkedJSONSchema,
dereferencedPaths: DereferencedPaths,
Expand Down
19 changes: 19 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,25 @@ via the \`patternProperty\` "${key.replace('*/', '*\\/')}".`
)
}

if (schema.required) {
asts = asts.concat(
map(
schema.required.filter(
_ => !schema.properties?.hasOwnProperty(_) && !schema.patternProperties?.hasOwnProperty(_),
),
key => {
return {
ast: {type: 'UNKNOWN'},
isPatternProperty: false,
isRequired: true,
isUnreachableDefinition: false,
keyName: key,
}
},
),
)
}

if (options.unreachableDefinitions) {
asts = asts.concat(
map(schema.$defs, (value, key: string) => {
Expand Down
4 changes: 2 additions & 2 deletions src/typesOfSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ const matchers: Record<SchemaType, (schema: JSONSchema) => boolean> = {
}
return 'enum' in schema
},
UNNAMED_SCHEMA() {
return false // Explicitly handled as the default case
UNNAMED_SCHEMA(schema) {
return !('$id' in schema) && ('patternProperties' in schema || 'properties' in schema || 'required' in schema)
},
UNTYPED_ARRAY(schema) {
return schema.type === 'array' && !('items' in schema)
Expand Down
Loading

0 comments on commit b3d8d5f

Please sign in to comment.