Skip to content

Commit

Permalink
fix drilling down struct looking for additionalProperties (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp committed Jun 23, 2021
1 parent 9f8b1ac commit 5ffbbe3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
42 changes: 42 additions & 0 deletions openapi3/issue376_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package openapi3

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestIssue376(t *testing.T) {
spec := []byte(`
openapi: 3.0.0
components:
schemas:
schema1:
type: object
additionalProperties:
type: string
schema2:
type: object
properties:
prop:
$ref: '#/components/schemas/schema1/additionalProperties'
paths: {}
info:
title: An API
version: 1.2.3.4
`)

loader := NewLoader()

doc, err := loader.LoadFromData(spec)
require.NoError(t, err)

err = doc.Validate(loader.Context)
require.NoError(t, err)

require.Equal(t, "An API", doc.Info.Title)
require.Equal(t, 2, len(doc.Components.Schemas))
require.Equal(t, 0, len(doc.Paths))

require.Equal(t, "string", doc.Components.Schemas["schema2"].Value.Properties["prop"].Value.Type)
}
15 changes: 15 additions & 0 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ func (loader *Loader) resolveComponent(
}
var cursor interface{}
if cursor, err = drill(doc); err != nil {
if path == nil {
return nil, err
}
var err2 error
data, err2 := loader.readURL(path)
if err2 != nil {
Expand Down Expand Up @@ -346,6 +349,14 @@ func (loader *Loader) resolveComponent(
}

func drillIntoField(cursor interface{}, fieldName string) (interface{}, error) {
// Special case due to multijson
if s, ok := cursor.(*SchemaRef); ok && fieldName == "additionalProperties" {
if ap := s.Value.AdditionalProperties; ap != nil {
return ap, nil
}
return s.Value.AdditionalPropertiesAllowed, nil
}

switch val := reflect.Indirect(reflect.ValueOf(cursor)); val.Kind() {
case reflect.Map:
elementValue := val.MapIndex(reflect.ValueOf(fieldName))
Expand All @@ -372,6 +383,10 @@ func drillIntoField(cursor interface{}, fieldName string) (interface{}, error) {
field := val.Type().Field(i)
tagValue := field.Tag.Get("yaml")
yamlKey := strings.Split(tagValue, ",")[0]
if yamlKey == "-" {
tagValue := field.Tag.Get("multijson")
yamlKey = strings.Split(tagValue, ",")[0]
}
if yamlKey == fieldName {
return val.Field(i).Interface(), nil
}
Expand Down
4 changes: 2 additions & 2 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type Schema struct {
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`

// Object-related, here for struct compactness
AdditionalPropertiesAllowed *bool `json:"-" multijson:"additionalProperties,omitempty" yaml:"-"`
AdditionalPropertiesAllowed *bool `multijson:"additionalProperties,omitempty" json:"-" yaml:"-"`
// Array-related, here for struct compactness
UniqueItems bool `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"`
// Number-related, here for struct compactness
Expand Down Expand Up @@ -145,7 +145,7 @@ type Schema struct {
Properties Schemas `json:"properties,omitempty" yaml:"properties,omitempty"`
MinProps uint64 `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
MaxProps *uint64 `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
AdditionalProperties *SchemaRef `json:"-" multijson:"additionalProperties,omitempty" yaml:"-"`
AdditionalProperties *SchemaRef `multijson:"additionalProperties,omitempty" json:"-" yaml:"-"`
Discriminator *Discriminator `json:"discriminator,omitempty" yaml:"discriminator,omitempty"`
}

Expand Down

0 comments on commit 5ffbbe3

Please sign in to comment.