Skip to content

Commit

Permalink
openapi3: handle tangible *nil in Schema.IsEmpty impl (#858)
Browse files Browse the repository at this point in the history
* openapi3: handle tangible *nil in Schema.IsEmpty impl

Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>

* openapi3: refacto PathItem.isEmpty

Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>

* notes on that impl

Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>

---------

Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Nov 25, 2023
1 parent a53cd59 commit 49395dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
14 changes: 1 addition & 13 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,19 +974,7 @@ func (loader *Loader) resolvePathItemRef(doc *T, pathItem *PathItem, documentPat
return
}
if ref := pathItem.Ref; ref != "" {
if pathItem.Summary != "" ||
pathItem.Description != "" ||
pathItem.Connect != nil ||
pathItem.Delete != nil ||
pathItem.Get != nil ||
pathItem.Head != nil ||
pathItem.Options != nil ||
pathItem.Patch != nil ||
pathItem.Post != nil ||
pathItem.Put != nil ||
pathItem.Trace != nil ||
len(pathItem.Servers) != 0 ||
len(pathItem.Parameters) != 0 {
if !pathItem.isEmpty() {
return
}
if isSingleRefElement(ref) {
Expand Down
19 changes: 19 additions & 0 deletions openapi3/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,22 @@ func (pathItem *PathItem) Validate(ctx context.Context, opts ...ValidationOption

return validateExtensions(ctx, pathItem.Extensions)
}

// isEmpty's introduced in 546590b1
func (pathItem *PathItem) isEmpty() bool {
// NOTE: ignores pathItem.Extensions
// NOTE: ignores pathItem.Ref
return pathItem.Summary == "" &&
pathItem.Description == "" &&
pathItem.Connect == nil &&
pathItem.Delete == nil &&
pathItem.Get == nil &&
pathItem.Head == nil &&
pathItem.Options == nil &&
pathItem.Patch == nil &&
pathItem.Post == nil &&
pathItem.Put == nil &&
pathItem.Trace == nil &&
len(pathItem.Servers) == 0 &&
len(pathItem.Parameters) == 0
}
14 changes: 7 additions & 7 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,35 +790,35 @@ func (schema *Schema) IsEmpty() bool {
schema.MinProps != 0 || schema.MaxProps != nil {
return false
}
if n := schema.Not; n != nil && !n.Value.IsEmpty() {
if n := schema.Not; n != nil && n.Value != nil && !n.Value.IsEmpty() {
return false
}
if ap := schema.AdditionalProperties.Schema; ap != nil && !ap.Value.IsEmpty() {
if ap := schema.AdditionalProperties.Schema; ap != nil && ap.Value != nil && !ap.Value.IsEmpty() {
return false
}
if apa := schema.AdditionalProperties.Has; apa != nil && !*apa {
return false
}
if items := schema.Items; items != nil && !items.Value.IsEmpty() {
if items := schema.Items; items != nil && items.Value != nil && !items.Value.IsEmpty() {
return false
}
for _, s := range schema.Properties {
if !s.Value.IsEmpty() {
if ss := s.Value; ss != nil && !ss.IsEmpty() {
return false
}
}
for _, s := range schema.OneOf {
if !s.Value.IsEmpty() {
if ss := s.Value; ss != nil && !ss.IsEmpty() {
return false
}
}
for _, s := range schema.AnyOf {
if !s.Value.IsEmpty() {
if ss := s.Value; ss != nil && !ss.IsEmpty() {
return false
}
}
for _, s := range schema.AllOf {
if !s.Value.IsEmpty() {
if ss := s.Value; ss != nil && !ss.IsEmpty() {
return false
}
}
Expand Down

0 comments on commit 49395dd

Please sign in to comment.