Skip to content

Commit 5ffbbe3

Browse files
authored
fix drilling down struct looking for additionalProperties (#377)
1 parent 9f8b1ac commit 5ffbbe3

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

openapi3/issue376_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package openapi3
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestIssue376(t *testing.T) {
10+
spec := []byte(`
11+
openapi: 3.0.0
12+
components:
13+
schemas:
14+
schema1:
15+
type: object
16+
additionalProperties:
17+
type: string
18+
schema2:
19+
type: object
20+
properties:
21+
prop:
22+
$ref: '#/components/schemas/schema1/additionalProperties'
23+
paths: {}
24+
info:
25+
title: An API
26+
version: 1.2.3.4
27+
`)
28+
29+
loader := NewLoader()
30+
31+
doc, err := loader.LoadFromData(spec)
32+
require.NoError(t, err)
33+
34+
err = doc.Validate(loader.Context)
35+
require.NoError(t, err)
36+
37+
require.Equal(t, "An API", doc.Info.Title)
38+
require.Equal(t, 2, len(doc.Components.Schemas))
39+
require.Equal(t, 0, len(doc.Paths))
40+
41+
require.Equal(t, "string", doc.Components.Schemas["schema2"].Value.Properties["prop"].Value.Type)
42+
}

openapi3/loader.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ func (loader *Loader) resolveComponent(
305305
}
306306
var cursor interface{}
307307
if cursor, err = drill(doc); err != nil {
308+
if path == nil {
309+
return nil, err
310+
}
308311
var err2 error
309312
data, err2 := loader.readURL(path)
310313
if err2 != nil {
@@ -346,6 +349,14 @@ func (loader *Loader) resolveComponent(
346349
}
347350

348351
func drillIntoField(cursor interface{}, fieldName string) (interface{}, error) {
352+
// Special case due to multijson
353+
if s, ok := cursor.(*SchemaRef); ok && fieldName == "additionalProperties" {
354+
if ap := s.Value.AdditionalProperties; ap != nil {
355+
return ap, nil
356+
}
357+
return s.Value.AdditionalPropertiesAllowed, nil
358+
}
359+
349360
switch val := reflect.Indirect(reflect.ValueOf(cursor)); val.Kind() {
350361
case reflect.Map:
351362
elementValue := val.MapIndex(reflect.ValueOf(fieldName))
@@ -372,6 +383,10 @@ func drillIntoField(cursor interface{}, fieldName string) (interface{}, error) {
372383
field := val.Type().Field(i)
373384
tagValue := field.Tag.Get("yaml")
374385
yamlKey := strings.Split(tagValue, ",")[0]
386+
if yamlKey == "-" {
387+
tagValue := field.Tag.Get("multijson")
388+
yamlKey = strings.Split(tagValue, ",")[0]
389+
}
375390
if yamlKey == fieldName {
376391
return val.Field(i).Interface(), nil
377392
}

openapi3/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type Schema struct {
110110
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
111111

112112
// Object-related, here for struct compactness
113-
AdditionalPropertiesAllowed *bool `json:"-" multijson:"additionalProperties,omitempty" yaml:"-"`
113+
AdditionalPropertiesAllowed *bool `multijson:"additionalProperties,omitempty" json:"-" yaml:"-"`
114114
// Array-related, here for struct compactness
115115
UniqueItems bool `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"`
116116
// Number-related, here for struct compactness
@@ -145,7 +145,7 @@ type Schema struct {
145145
Properties Schemas `json:"properties,omitempty" yaml:"properties,omitempty"`
146146
MinProps uint64 `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
147147
MaxProps *uint64 `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
148-
AdditionalProperties *SchemaRef `json:"-" multijson:"additionalProperties,omitempty" yaml:"-"`
148+
AdditionalProperties *SchemaRef `multijson:"additionalProperties,omitempty" json:"-" yaml:"-"`
149149
Discriminator *Discriminator `json:"discriminator,omitempty" yaml:"discriminator,omitempty"`
150150
}
151151

0 commit comments

Comments
 (0)