Skip to content

Commit b3d8d5f

Browse files
committed
[Bugfix] Fix complex intersections of allOf/anyOf/properties/required (fix #381)
1 parent 6adcad9 commit b3d8d5f

File tree

8 files changed

+4854
-508
lines changed

8 files changed

+4854
-508
lines changed

src/normalizer.ts

+14
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,20 @@ rules.set('Transform const to singleton enum', schema => {
222222
}
223223
})
224224

225+
// @see https://json-schema.org/understanding-json-schema/reference/combining#factoring-schemas
226+
rules.set('Propagate additionalProperties=false to all factors', schema => {
227+
const parent = schema[Parent]
228+
if (!parent || parent.additionalProperties !== false) {
229+
return
230+
}
231+
232+
if (schema.hasOwnProperty('additionalProperties')) {
233+
return
234+
}
235+
236+
schema.additionalProperties = false
237+
})
238+
225239
export function normalize(
226240
rootSchema: LinkedJSONSchema,
227241
dereferencedPaths: DereferencedPaths,

src/parser.ts

+19
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,25 @@ via the \`patternProperty\` "${key.replace('*/', '*\\/')}".`
427427
)
428428
}
429429

430+
if (schema.required) {
431+
asts = asts.concat(
432+
map(
433+
schema.required.filter(
434+
_ => !schema.properties?.hasOwnProperty(_) && !schema.patternProperties?.hasOwnProperty(_),
435+
),
436+
key => {
437+
return {
438+
ast: {type: 'UNKNOWN'},
439+
isPatternProperty: false,
440+
isRequired: true,
441+
isUnreachableDefinition: false,
442+
keyName: key,
443+
}
444+
},
445+
),
446+
)
447+
}
448+
430449
if (options.unreachableDefinitions) {
431450
asts = asts.concat(
432451
map(schema.$defs, (value, key: string) => {

src/typesOfSchema.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ const matchers: Record<SchemaType, (schema: JSONSchema) => boolean> = {
140140
}
141141
return 'enum' in schema
142142
},
143-
UNNAMED_SCHEMA() {
144-
return false // Explicitly handled as the default case
143+
UNNAMED_SCHEMA(schema) {
144+
return !('$id' in schema) && ('patternProperties' in schema || 'properties' in schema || 'required' in schema)
145145
},
146146
UNTYPED_ARRAY(schema) {
147147
return schema.type === 'array' && !('items' in schema)

0 commit comments

Comments
 (0)