| 1 |
// Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package strict |
| 5 |
|
| 6 |
import ( |
| 7 |
"regexp" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/pb33f/libopenapi/datamodel/high/base" |
| 11 |
) |
| 12 |
|
| 13 |
// validatePolymorphic handles allOf, oneOf, and anyOf schemas. |
| 14 |
// For allOf: merge all schemas and validate against all. |
| 15 |
// For oneOf/anyOf: find the matching variant and validate against it. |
| 16 |
func (v *Validator) validatePolymorphic(ctx *traversalContext, schema *base.Schema, data map[string]any) []UndeclaredValue { |
| 17 |
var undeclared []UndeclaredValue |
| 18 |
|
| 19 |
// Handle allOf first - data must match ALL schemas |
| 20 |
if len(schema.AllOf) > 0 { |
| 21 |
undeclared = append(undeclared, v.validateAllOf(ctx, schema, data)...) |
| 22 |
} |
| 23 |
|
| 24 |
// Handle oneOf - data must match exactly ONE schema |
| 25 |
if len(schema.OneOf) > 0 { |
| 26 |
undeclared = append(undeclared, v.validateOneOf(ctx, schema, data)...) |
| 27 |
} |
| 28 |
|
| 29 |
// Handle anyOf - data must match at least ONE schema |
| 30 |
if len(schema.AnyOf) > 0 { |
| 31 |
undeclared = append(undeclared, v.validateAnyOf(ctx, schema, data)...) |
| 32 |
} |
| 33 |
|
| 34 |
// Also validate any direct properties on the parent schema |
| 35 |
if schema.Properties != nil { |
| 36 |
declared, patterns := v.collectDeclaredProperties(schema, data) |
| 37 |
|
| 38 |
// Check properties that aren't handled by allOf/oneOf/anyOf |
| 39 |
for propName := range data { |
| 40 |
// Skip if declared directly or via patterns |
| 41 |
if isPropertyDeclared(propName, declared, patterns) { |
| 42 |
continue |
| 43 |
} |
| 44 |
|
| 45 |
// Check if it's declared in any of the allOf schemas |
| 46 |
if v.isPropertyDeclaredInAllOf(schema.AllOf, propName) { |
| 47 |
continue |
| 48 |
} |
| 49 |
|
| 50 |
// For oneOf/anyOf, we've already validated against the matching variant |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return undeclared |
| 55 |
} |
| 56 |
|
| 57 |
// validateAllOf validates data against all schemas in allOf. |
| 58 |
// Collects properties from all schemas as declared. |
| 59 |
func (v *Validator) validateAllOf(ctx *traversalContext, schema *base.Schema, data map[string]any) []UndeclaredValue { |
| 60 |
var undeclared []UndeclaredValue |
| 61 |
|
| 62 |
// Collect declared properties from ALL schemas in allOf |
| 63 |
allDeclared := make(map[string]*declaredProperty) |
| 64 |
var allPatterns []*regexp.Regexp |
| 65 |
|
| 66 |
for _, schemaProxy := range schema.AllOf { |
| 67 |
if schemaProxy == nil { |
| 68 |
continue |
| 69 |
} |
| 70 |
|
| 71 |
subSchema := schemaProxy.Schema() |
| 72 |
if subSchema == nil { |
| 73 |
continue |
| 74 |
} |
| 75 |
|
| 76 |
declared, patterns := v.collectDeclaredProperties(subSchema, data) |
| 77 |
for name, prop := range declared { |
| 78 |
if _, exists := allDeclared[name]; !exists { |
| 79 |
allDeclared[name] = prop |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
allPatterns = append(allPatterns, patterns...) |
| 84 |
} |
| 85 |
|
| 86 |
// collect from parent schema |
| 87 |
declared, patterns := v.collectDeclaredProperties(schema, data) |
| 88 |
for name, prop := range declared { |
| 89 |
if _, exists := allDeclared[name]; !exists { |
| 90 |
allDeclared[name] = prop |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
allPatterns = append(allPatterns, patterns...) |
| 95 |
|
| 96 |
// check if strict mode should report for this combined schema |
| 97 |
if !v.shouldReportUndeclaredForAllOf(schema) { |
| 98 |
// Still recurse into declared properties |
| 99 |
return v.recurseIntoAllOfDeclaredProperties(ctx, schema.AllOf, data, allDeclared) |
| 100 |
} |
| 101 |
|
| 102 |
// Check each property in data |
| 103 |
for propName, propValue := range data { |
| 104 |
propPath := buildPath(ctx.path, propName) |
| 105 |
propCtx := ctx.withPath(propPath) |
| 106 |
|
| 107 |
if propCtx.shouldIgnore() { |
| 108 |
continue |
| 109 |
} |
| 110 |
|
| 111 |
// Check if declared in merged schema |
| 112 |
if isPropertyDeclared(propName, allDeclared, allPatterns) { |
| 113 |
// Recurse into the property |
| 114 |
propSchema := v.findPropertySchemaInAllOf(schema.AllOf, propName, allDeclared) |
| 115 |
if propSchema != nil { |
| 116 |
if violation, ok := v.checkReadWriteOnlyViolation(propPath, propName, propValue, propSchema, ctx.direction); ok { |
| 117 |
undeclared = append(undeclared, violation) |
| 118 |
continue |
| 119 |
} |
| 120 |
if v.shouldSkipProperty(propSchema, ctx.direction) { |
| 121 |
continue |
| 122 |
} |
| 123 |
undeclared = append(undeclared, v.validateValue(propCtx, propSchema, propValue)...) |
| 124 |
} |
| 125 |
continue |
| 126 |
} |
| 127 |
|
| 128 |
// Not declared - report as undeclared |
| 129 |
undeclared = append(undeclared, |
| 130 |
newUndeclaredProperty(propPath, propName, propValue, getDeclaredPropertyNames(allDeclared), ctx.direction, schema)) |
| 131 |
} |
| 132 |
|
| 133 |
return undeclared |
| 134 |
} |
| 135 |
|
| 136 |
// validateOneOf finds the matching oneOf variant and validates against it. |
| 137 |
// Parent schema properties are merged with the variant's properties. |
| 138 |
func (v *Validator) validateOneOf(ctx *traversalContext, schema *base.Schema, data map[string]any) []UndeclaredValue { |
| 139 |
var matchingVariant *base.Schema |
| 140 |
|
| 141 |
// discriminator is present, use it to select the variant |
| 142 |
if schema.Discriminator != nil { |
| 143 |
matchingVariant = v.selectByDiscriminator(schema, schema.OneOf, data) |
| 144 |
} |
| 145 |
|
| 146 |
// no discriminator or no match: find matching variant by validation |
| 147 |
if matchingVariant == nil { |
| 148 |
matchingVariant = v.findMatchingVariant(schema.OneOf, data) |
| 149 |
} |
| 150 |
|
| 151 |
if matchingVariant == nil { |
| 152 |
// No match found - base validation would report this error |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
// Validate against variant, but filter out properties declared in parent |
| 157 |
return v.validateVariantWithParent(ctx, schema, matchingVariant, data) |
| 158 |
} |
| 159 |
|
| 160 |
// validateAnyOf finds matching anyOf variants and validates against them. |
| 161 |
// Parent schema properties are merged with the variant's properties. |
| 162 |
func (v *Validator) validateAnyOf(ctx *traversalContext, schema *base.Schema, data map[string]any) []UndeclaredValue { |
| 163 |
var matchingVariant *base.Schema |
| 164 |
|
| 165 |
// If discriminator is present, use it to select the variant |
| 166 |
if schema.Discriminator != nil { |
| 167 |
matchingVariant = v.selectByDiscriminator(schema, schema.AnyOf, data) |
| 168 |
} |
| 169 |
|
| 170 |
// No discriminator or no match: find matching variant by validation |
| 171 |
if matchingVariant == nil { |
| 172 |
matchingVariant = v.findMatchingVariant(schema.AnyOf, data) |
| 173 |
} |
| 174 |
|
| 175 |
if matchingVariant == nil { |
| 176 |
// No match found - base validation would report this error |
| 177 |
return nil |
| 178 |
} |
| 179 |
|
| 180 |
// Validate against variant, but filter out properties declared in parent |
| 181 |
return v.validateVariantWithParent(ctx, schema, matchingVariant, data) |
| 182 |
} |
| 183 |
|
| 184 |
// validateVariantWithParent validates data against a variant schema while also |
| 185 |
// considering properties declared in the parent schema. This ensures parent |
| 186 |
// properties are not reported as undeclared when using oneOf/anyOf. |
| 187 |
func (v *Validator) validateVariantWithParent(ctx *traversalContext, parent *base.Schema, variant *base.Schema, data map[string]any) []UndeclaredValue { |
| 188 |
var undeclared []UndeclaredValue |
| 189 |
|
| 190 |
// Collect declared properties from parent schema |
| 191 |
parentDeclared, parentPatterns := v.collectDeclaredProperties(parent, data) |
| 192 |
|
| 193 |
// Collect declared properties from variant schema |
| 194 |
variantDeclared, variantPatterns := v.collectDeclaredProperties(variant, data) |
| 195 |
|
| 196 |
// Merge: parent + variant |
| 197 |
allDeclared := make(map[string]*declaredProperty) |
| 198 |
for name, prop := range parentDeclared { |
| 199 |
allDeclared[name] = prop |
| 200 |
} |
| 201 |
for name, prop := range variantDeclared { |
| 202 |
allDeclared[name] = prop |
| 203 |
} |
| 204 |
allPatterns := append(parentPatterns, variantPatterns...) |
| 205 |
|
| 206 |
// Check if we should report undeclared (skip if additionalProperties: false) |
| 207 |
if !v.shouldReportUndeclared(variant) && !v.shouldReportUndeclared(parent) { |
| 208 |
// Still recurse into declared properties |
| 209 |
return v.recurseIntoDeclaredPropertiesWithMerged(ctx, variant, parent, data, allDeclared) |
| 210 |
} |
| 211 |
|
| 212 |
// Check each property in data |
| 213 |
for propName, propValue := range data { |
| 214 |
propPath := buildPath(ctx.path, propName) |
| 215 |
propCtx := ctx.withPath(propPath) |
| 216 |
|
| 217 |
if propCtx.shouldIgnore() { |
| 218 |
continue |
| 219 |
} |
| 220 |
|
| 221 |
// Check if declared in merged schema (parent + variant) |
| 222 |
if isPropertyDeclared(propName, allDeclared, allPatterns) { |
| 223 |
// Find the property schema (prefer variant, fallback to parent) |
| 224 |
propSchema := v.findPropertySchemaInMerged(variant, parent, propName, allDeclared) |
| 225 |
if propSchema != nil { |
| 226 |
if violation, ok := v.checkReadWriteOnlyViolation(propPath, propName, propValue, propSchema, ctx.direction); ok { |
| 227 |
undeclared = append(undeclared, violation) |
| 228 |
continue |
| 229 |
} |
| 230 |
if v.shouldSkipProperty(propSchema, ctx.direction) { |
| 231 |
continue |
| 232 |
} |
| 233 |
undeclared = append(undeclared, v.validateValue(propCtx, propSchema, propValue)...) |
| 234 |
} |
| 235 |
continue |
| 236 |
} |
| 237 |
|
| 238 |
// Not declared - report as undeclared |
| 239 |
// Use variant schema location if available, otherwise fall back to parent |
| 240 |
locationSchema := variant |
| 241 |
if locationSchema == nil || locationSchema.GoLow() == nil { |
| 242 |
locationSchema = parent |
| 243 |
} |
| 244 |
undeclared = append(undeclared, |
| 245 |
newUndeclaredProperty(propPath, propName, propValue, getDeclaredPropertyNames(allDeclared), ctx.direction, locationSchema)) |
| 246 |
} |
| 247 |
|
| 248 |
return undeclared |
| 249 |
} |
| 250 |
|
| 251 |
// findPropertySchemaInMerged finds the schema for a property, preferring variant over parent. |
| 252 |
// Checks explicit properties first, then patternProperties. |
| 253 |
func (v *Validator) findPropertySchemaInMerged(variant, parent *base.Schema, propName string, declared map[string]*declaredProperty) *base.Schema { |
| 254 |
// Check explicit declared first |
| 255 |
if prop, ok := declared[propName]; ok && prop.proxy != nil { |
| 256 |
return prop.proxy.Schema() |
| 257 |
} |
| 258 |
|
| 259 |
// Check variant schema explicit properties |
| 260 |
if variant != nil && variant.Properties != nil { |
| 261 |
if propProxy, exists := variant.Properties.Get(propName); exists && propProxy != nil { |
| 262 |
return propProxy.Schema() |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
// Check parent schema explicit properties |
| 267 |
if parent != nil && parent.Properties != nil { |
| 268 |
if propProxy, exists := parent.Properties.Get(propName); exists && propProxy != nil { |
| 269 |
return propProxy.Schema() |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
// Check variant patternProperties |
| 274 |
if variant != nil { |
| 275 |
if propProxy := v.getPatternPropertySchema(variant, propName); propProxy != nil { |
| 276 |
return propProxy.Schema() |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
// Check parent patternProperties |
| 281 |
if parent != nil { |
| 282 |
if propProxy := v.getPatternPropertySchema(parent, propName); propProxy != nil { |
| 283 |
return propProxy.Schema() |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
return nil |
| 288 |
} |
| 289 |
|
| 290 |
// recurseIntoDeclaredPropertiesWithMerged recurses into properties from merged parent+variant. |
| 291 |
func (v *Validator) recurseIntoDeclaredPropertiesWithMerged(ctx *traversalContext, variant, parent *base.Schema, data map[string]any, declared map[string]*declaredProperty) []UndeclaredValue { |
| 292 |
var undeclared []UndeclaredValue |
| 293 |
|
| 294 |
for propName, propValue := range data { |
| 295 |
propPath := buildPath(ctx.path, propName) |
| 296 |
propCtx := ctx.withPath(propPath) |
| 297 |
|
| 298 |
if propCtx.shouldIgnore() { |
| 299 |
continue |
| 300 |
} |
| 301 |
|
| 302 |
propSchema := v.findPropertySchemaInMerged(variant, parent, propName, declared) |
| 303 |
if propSchema != nil { |
| 304 |
if violation, ok := v.checkReadWriteOnlyViolation(propPath, propName, propValue, propSchema, ctx.direction); ok { |
| 305 |
undeclared = append(undeclared, violation) |
| 306 |
continue |
| 307 |
} |
| 308 |
if v.shouldSkipProperty(propSchema, ctx.direction) { |
| 309 |
continue |
| 310 |
} |
| 311 |
undeclared = append(undeclared, v.validateValue(propCtx, propSchema, propValue)...) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
return undeclared |
| 316 |
} |
| 317 |
|
| 318 |
// selectByDiscriminator uses the discriminator to select the appropriate variant. |
| 319 |
func (v *Validator) selectByDiscriminator(schema *base.Schema, variants []*base.SchemaProxy, data map[string]any) *base.Schema { |
| 320 |
if schema.Discriminator == nil { |
| 321 |
return nil |
| 322 |
} |
| 323 |
|
| 324 |
propName := schema.Discriminator.PropertyName |
| 325 |
if propName == "" { |
| 326 |
return nil |
| 327 |
} |
| 328 |
|
| 329 |
discriminatorValue, ok := data[propName] |
| 330 |
if !ok { |
| 331 |
return nil |
| 332 |
} |
| 333 |
|
| 334 |
valueStr, ok := discriminatorValue.(string) |
| 335 |
if !ok { |
| 336 |
return nil |
| 337 |
} |
| 338 |
|
| 339 |
// check mapping first |
| 340 |
if schema.Discriminator.Mapping != nil { |
| 341 |
for pair := schema.Discriminator.Mapping.First(); pair != nil; pair = pair.Next() { |
| 342 |
if pair.Key() == valueStr { |
| 343 |
// The mapping value is a reference like "#/components/schemas/Dog" |
| 344 |
mappedRef := pair.Value() |
| 345 |
for _, variantProxy := range variants { |
| 346 |
if variantProxy.IsReference() && variantProxy.GetReference() == mappedRef { |
| 347 |
return variantProxy.Schema() |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
// no mapping match, try to match by schema name in reference |
| 355 |
for _, variantProxy := range variants { |
| 356 |
if variantProxy.IsReference() { |
| 357 |
ref := variantProxy.GetReference() |
| 358 |
// Extract schema name from reference like "#/components/schemas/Dog" |
| 359 |
parts := strings.Split(ref, "/") |
| 360 |
if len(parts) > 0 && parts[len(parts)-1] == valueStr { |
| 361 |
return variantProxy.Schema() |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
return nil |
| 367 |
} |
| 368 |
|
| 369 |
// findMatchingVariant finds the first variant that the data validates against. |
| 370 |
func (v *Validator) findMatchingVariant(variants []*base.SchemaProxy, data map[string]any) *base.Schema { |
| 371 |
for _, variantProxy := range variants { |
| 372 |
if variantProxy == nil { |
| 373 |
continue |
| 374 |
} |
| 375 |
|
| 376 |
variantSchema := variantProxy.Schema() |
| 377 |
if variantSchema == nil { |
| 378 |
continue |
| 379 |
} |
| 380 |
|
| 381 |
matches, _ := v.dataMatchesSchema(variantSchema, data) |
| 382 |
if matches { |
| 383 |
return variantSchema |
| 384 |
} |
| 385 |
} |
| 386 |
return nil |
| 387 |
} |
| 388 |
|
| 389 |
// isPropertyDeclaredInAllOf checks if a property is declared in any allOf schema. |
| 390 |
func (v *Validator) isPropertyDeclaredInAllOf(allOf []*base.SchemaProxy, propName string) bool { |
| 391 |
for _, schemaProxy := range allOf { |
| 392 |
if schemaProxy == nil { |
| 393 |
continue |
| 394 |
} |
| 395 |
|
| 396 |
subSchema := schemaProxy.Schema() |
| 397 |
if subSchema == nil { |
| 398 |
continue |
| 399 |
} |
| 400 |
|
| 401 |
if subSchema.Properties != nil { |
| 402 |
if _, exists := subSchema.Properties.Get(propName); exists { |
| 403 |
return true |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
return false |
| 408 |
} |
| 409 |
|
| 410 |
// shouldReportUndeclaredForAllOf checks if any schema in allOf disables additional properties. |
| 411 |
func (v *Validator) shouldReportUndeclaredForAllOf(schema *base.Schema) bool { |
| 412 |
// Check parent schema |
| 413 |
if schema.AdditionalProperties != nil && schema.AdditionalProperties.IsB() && !schema.AdditionalProperties.B { |
| 414 |
return false |
| 415 |
} |
| 416 |
|
| 417 |
// Check each allOf schema |
| 418 |
for _, schemaProxy := range schema.AllOf { |
| 419 |
if schemaProxy == nil { |
| 420 |
continue |
| 421 |
} |
| 422 |
|
| 423 |
subSchema := schemaProxy.Schema() |
| 424 |
if subSchema == nil { |
| 425 |
continue |
| 426 |
} |
| 427 |
|
| 428 |
if subSchema.AdditionalProperties != nil && subSchema.AdditionalProperties.IsB() && !subSchema.AdditionalProperties.B { |
| 429 |
return false |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
return true |
| 434 |
} |
| 435 |
|
| 436 |
// findPropertySchemaInAllOf finds the schema for a property in allOf schemas. |
| 437 |
func (v *Validator) findPropertySchemaInAllOf(allOf []*base.SchemaProxy, propName string, declared map[string]*declaredProperty) *base.Schema { |
| 438 |
// Check explicit declared first |
| 439 |
if prop, ok := declared[propName]; ok && prop.proxy != nil { |
| 440 |
return prop.proxy.Schema() |
| 441 |
} |
| 442 |
|
| 443 |
// Search in allOf schemas |
| 444 |
for _, schemaProxy := range allOf { |
| 445 |
if schemaProxy == nil { |
| 446 |
continue |
| 447 |
} |
| 448 |
|
| 449 |
subSchema := schemaProxy.Schema() |
| 450 |
if subSchema == nil { |
| 451 |
continue |
| 452 |
} |
| 453 |
|
| 454 |
if subSchema.Properties != nil { |
| 455 |
if propProxy, exists := subSchema.Properties.Get(propName); exists && propProxy != nil { |
| 456 |
return propProxy.Schema() |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
return nil |
| 462 |
} |
| 463 |
|
| 464 |
// recurseIntoAllOfDeclaredProperties recurses into properties without checking for undeclared. |
| 465 |
func (v *Validator) recurseIntoAllOfDeclaredProperties(ctx *traversalContext, allOf []*base.SchemaProxy, data map[string]any, declared map[string]*declaredProperty) []UndeclaredValue { |
| 466 |
var undeclared []UndeclaredValue |
| 467 |
|
| 468 |
for propName, propValue := range data { |
| 469 |
propPath := buildPath(ctx.path, propName) |
| 470 |
propCtx := ctx.withPath(propPath) |
| 471 |
|
| 472 |
if propCtx.shouldIgnore() { |
| 473 |
continue |
| 474 |
} |
| 475 |
|
| 476 |
propSchema := v.findPropertySchemaInAllOf(allOf, propName, declared) |
| 477 |
if propSchema != nil { |
| 478 |
if violation, ok := v.checkReadWriteOnlyViolation(propPath, propName, propValue, propSchema, ctx.direction); ok { |
| 479 |
undeclared = append(undeclared, violation) |
| 480 |
continue |
| 481 |
} |
| 482 |
if v.shouldSkipProperty(propSchema, ctx.direction) { |
| 483 |
continue |
| 484 |
} |
| 485 |
undeclared = append(undeclared, v.validateValue(propCtx, propSchema, propValue)...) |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
return undeclared |
| 490 |
} |