@@ -148,21 +148,29 @@ func diffNode(path string, base, head Schema, out *[]string) {
148148 diffNode (child , baseProps [name ], hp , out )
149149 }
150150
151- // additionalProperties: restricting it to false rejects previously-allowed
152- // undeclared fields — a breaking change that a boolean value carries and a
153- // schema recursion would miss. Otherwise recurse only when BOTH sides carry
154- // a map schema: a child schema present only in base is a relaxation (its
155- // removal loosens the contract), and diffing it against a nil head would
156- // misreport that relaxation as a break.
157- if base ["additionalProperties" ] != false && head ["additionalProperties" ] == false {
151+ // additionalProperties governs undeclared fields; its permissiveness runs
152+ // false (none) < schema (matching) < true/absent (any).
153+ // - open -> false: breaking (fields once accepted now rejected)
154+ // - open -> schema: recurse; adding a schema to a previously-open
155+ // map is itself a restriction, caught by diffing
156+ // the new schema against an empty (unconstrained)
157+ // base.
158+ // - base false -> *: relaxation (base accepted nothing), so safe.
159+ // - open -> true/absent: relaxation, safe.
160+ baseOpenAP := base ["additionalProperties" ] != false
161+ if baseOpenAP && head ["additionalProperties" ] == false {
158162 * out = append (* out , fmt .Sprintf ("%s: additionalProperties restricted to false (undeclared fields no longer accepted)" , path ))
159- } else if b , h , ok := bothMapSchemas (base , head , "additionalProperties" ); ok {
160- diffNode (path + "{}" , b , h , out )
163+ } else if baseOpenAP {
164+ if b , h , ok := childSchemas (base , head , "additionalProperties" ); ok {
165+ diffNode (path + "{}" , b , h , out )
166+ }
161167 }
162168
163- // items: recurse only when both sides define an item schema; removing items
164- // loosens an array's contract and is safe.
165- if b , h , ok := bothMapSchemas (base , head , "items" ); ok {
169+ // items: recurse whenever head defines an item schema. When base has none,
170+ // the head schema is diffed against an empty base so that constraining a
171+ // previously-unconstrained array element is caught; a base schema with no
172+ // head counterpart is a relaxation and is skipped.
173+ if b , h , ok := childSchemas (base , head , "items" ); ok {
166174 diffNode (path + "[]" , b , h , out )
167175 }
168176}
@@ -267,18 +275,25 @@ func schemaProps(s Schema) map[string]Schema {
267275 return out
268276}
269277
270- // bothMapSchemas extracts a named child schema (e.g. items, additionalProperties)
271- // from both nodes, reporting ok only when BOTH sides carry a schema object.
272- // Requiring both sides is deliberate: a child schema present only in base was
273- // removed on head, which loosens the contract (safe), and recursing into it
274- // against a nil head would misreport that relaxation as a break. A boolean
275- // additionalProperties (true/false) is handled separately by the caller.
276- func bothMapSchemas (base , head Schema , key string ) (Schema , Schema , bool ) {
277- b , bOK := base [key ].(map [string ]any )
278- h , hOK := head [key ].(map [string ]any )
279- if ! bOK || ! hOK {
278+ // childSchemas extracts a named child schema (items or additionalProperties)
279+ // for recursion. It reports ok only when HEAD carries a schema object; base's
280+ // counterpart is returned when present and an empty schema otherwise. The
281+ // asymmetry is deliberate:
282+ // - head schema, base schema -> diff them (nested changes).
283+ // - head schema, base none -> diff head against an empty base, so that
284+ // constraining a previously-unconstrained array element / map value
285+ // (base absent or true) surfaces as breaking.
286+ // - head none, base schema -> ok=false: removing a child schema loosens
287+ // the contract (relaxation), so it is skipped.
288+ //
289+ // A boolean additionalProperties (true/false) is not a schema object and is
290+ // handled separately by the caller.
291+ func childSchemas (base , head Schema , key string ) (Schema , Schema , bool ) {
292+ h , ok := head [key ].(map [string ]any )
293+ if ! ok {
280294 return nil , nil , false
281295 }
296+ b , _ := base [key ].(map [string ]any ) // nil -> empty (unconstrained) base schema
282297 return Schema (b ), Schema (h ), true
283298}
284299
0 commit comments