Skip to content

Commit b2d3ca4

Browse files
committed
feat: openapi3/modify: add SpecSetSchemaPropertiesOptional()
1 parent 2080cee commit b2d3ca4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

openapi3/modify/required.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package modify
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/grokify/simplego/type/stringsutil"
7+
8+
oas3 "github.com/getkin/kin-openapi/openapi3"
9+
)
10+
11+
var rxOptionalDefault = regexp.MustCompile(`(?i)\boptional\b`)
12+
13+
func SpecSetSchemaPropertiesOptional(spec *oas3.Swagger, rxOptional *regexp.Regexp) {
14+
if rxOptional == nil {
15+
return
16+
}
17+
for _, schemaRef := range spec.Components.Schemas {
18+
if len(schemaRef.Ref) == 0 && schemaRef.Value != nil {
19+
required := []string{}
20+
for propName, propRef := range schemaRef.Value.Properties {
21+
if len(propRef.Ref) == 0 && propRef.Value != nil {
22+
if len(propRef.Value.Description) > 0 &&
23+
!rxOptional.MatchString(propRef.Value.Description) {
24+
required = append(required, propName)
25+
}
26+
}
27+
}
28+
if len(required) > 1 {
29+
required = stringsutil.SliceCondenseSpace(required, true, true)
30+
}
31+
schemaRef.Value.Required = required
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)