Debian Codegraph @

strict/property_collector.go

raw
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
9 "github.com/pb33f/libopenapi/datamodel/high/base"
10 )
11
12 // declaredProperty holds information about a declared property in a schema.
13 type declaredProperty struct {
14 // proxy is the SchemaProxy for the property.
15 proxy *base.SchemaProxy
16 }
17
18 // collectDeclaredProperties gathers all property names that are declared in a schema.
19 // This includes explicit properties, patternProperties matches, and properties from
20 // dependentSchemas and if/then/else based on the actual data.
21 //
22 // Returns a map from property name to its declaration info, plus a slice of
23 // pattern regexes for patternProperties matching.
24 func (v *Validator) collectDeclaredProperties(
25 schema *base.Schema,
26 data map[string]any,
27 ) (declared map[string]*declaredProperty, patterns []*regexp.Regexp) {
28 declared = make(map[string]*declaredProperty)
29
30 if schema == nil {
31 return declared, nil
32 }
33
34 // explicit properties
35 if schema.Properties != nil {
36 for pair := schema.Properties.First(); pair != nil; pair = pair.Next() {
37 declared[pair.Key()] = &declaredProperty{
38 proxy: pair.Value(),
39 }
40 }
41 }
42
43 // pattern properties - use cached compiled patterns
44 if schema.PatternProperties != nil {
45 for pair := schema.PatternProperties.First(); pair != nil; pair = pair.Next() {
46 pattern := v.getCompiledPattern(pair.Key())
47 if pattern == nil {
48 continue
49 }
50 patterns = append(patterns, pattern)
51 }
52 }
53
54 // dependent schemas - if trigger property exists in data
55 if schema.DependentSchemas != nil {
56 for pair := schema.DependentSchemas.First(); pair != nil; pair = pair.Next() {
57 triggerProp := pair.Key()
58 if _, exists := data[triggerProp]; !exists {
59 continue
60 }
61 // trigger property exists, include dependent schema's properties
62 mergePropertiesIntoDeclared(declared, pair.Value().Schema())
63 }
64 }
65
66 // if/then/else
67 if schema.If != nil {
68 ifProxy := schema.If
69 ifSchema := ifProxy.Schema()
70 if ifSchema != nil {
71 matches, err := v.dataMatchesSchema(ifSchema, data)
72 if err != nil {
73 // schema compilation failed - log and use else branch
74 v.logger.Debug("strict: if schema compilation failed, using else branch", "error", err)
75 matches = false
76 }
77 if matches {
78 if schema.Then != nil {
79 mergePropertiesIntoDeclared(declared, schema.Then.Schema())
80 }
81 } else {
82 if schema.Else != nil {
83 mergePropertiesIntoDeclared(declared, schema.Else.Schema())
84 }
85 }
86 }
87 }
88
89 return declared, patterns
90 }
91
92 // mergePropertiesIntoDeclared merges properties from a schema's Properties map into
93 // the declared map. Only adds properties that are not already declared.
94 // This eliminates code duplication when collecting properties from multiple sources.
95 func mergePropertiesIntoDeclared(declared map[string]*declaredProperty, schema *base.Schema) {
96 if schema == nil || schema.Properties == nil {
97 return
98 }
99 for p := schema.Properties.First(); p != nil; p = p.Next() {
100 if _, alreadyDeclared := declared[p.Key()]; !alreadyDeclared {
101 declared[p.Key()] = &declaredProperty{
102 proxy: p.Value(),
103 }
104 }
105 }
106 }
107
108 // getDeclaredPropertyNames returns just the property names from declared properties.
109 func getDeclaredPropertyNames(declared map[string]*declaredProperty) []string {
110 if len(declared) == 0 {
111 return nil
112 }
113 names := make([]string, 0, len(declared))
114 for name := range declared {
115 names = append(names, name)
116 }
117 return names
118 }
119
120 // isPropertyDeclared checks if a property name is declared in the schema.
121 // A property is declared if:
122 // - It's in the explicit properties map
123 // - It matches any patternProperties regex
124 func isPropertyDeclared(name string, declared map[string]*declaredProperty, patterns []*regexp.Regexp) bool {
125 // check explicit properties
126 if _, ok := declared[name]; ok {
127 return true
128 }
129
130 // check pattern properties
131 for _, pattern := range patterns {
132 if pattern.MatchString(name) {
133 return true
134 }
135 }
136
137 return false
138 }
139
140 // getPropertySchema returns the SchemaProxy for a declared property.
141 // Returns nil if the property is not declared or is only matched by pattern.
142 func getPropertySchema(name string, declared map[string]*declaredProperty) *base.SchemaProxy {
143 // check explicit properties first
144 if dp, ok := declared[name]; ok && dp.proxy != nil {
145 return dp.proxy
146 }
147 return nil
148 }
149
150 // checkReadWriteOnlyViolation checks if a property violates readOnly/writeOnly rules
151 // when the corresponding rejection flag is enabled. Returns a violation and true if so.
152 func (v *Validator) checkReadWriteOnlyViolation(
153 path string, name string, value any,
154 schema *base.Schema, direction Direction,
155 ) (UndeclaredValue, bool) {
156 if schema == nil || v.options == nil {
157 return UndeclaredValue{}, false
158 }
159 if direction == DirectionRequest && v.options.StrictRejectReadOnly &&
160 schema.ReadOnly != nil && *schema.ReadOnly {
161 return newReadWriteOnlyViolation(path, name, value, direction, schema), true
162 }
163 if direction == DirectionResponse && v.options.StrictRejectWriteOnly &&
164 schema.WriteOnly != nil && *schema.WriteOnly {
165 return newReadWriteOnlyViolation(path, name, value, direction, schema), true
166 }
167 return UndeclaredValue{}, false
168 }
169
170 // shouldSkipProperty checks if a property should be skipped based on
171 // readOnly/writeOnly and the current validation direction.
172 func (v *Validator) shouldSkipProperty(schema *base.Schema, direction Direction) bool {
173 if schema == nil {
174 return false
175 }
176
177 // readOnly: skip in requests (should not be sent by client)
178 if direction == DirectionRequest && schema.ReadOnly != nil && *schema.ReadOnly {
179 return true
180 }
181
182 // writeOnly: skip in responses (should not be returned by server)
183 if direction == DirectionResponse && schema.WriteOnly != nil && *schema.WriteOnly {
184 return true
185 }
186
187 return false
188 }