Skip to content

Commit

Permalink
additionalProperties attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
danicc097 committed Feb 18, 2024
1 parent 2452118 commit f7bc1ca
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
12 changes: 10 additions & 2 deletions openapi3filter/req_resp_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,11 @@ func findNestedSchema(parentSchema *openapi3.SchemaRef, keys []string) (*openapi
for _, key := range keys {
propertySchema, ok := currentSchema.Value.Properties[key]
if !ok {
return nil, fmt.Errorf("nested schema for key %q not found", key)
if currentSchema.Value.AdditionalProperties.Schema == nil {
return nil, fmt.Errorf("nested schema for key %q not found", key)
}
currentSchema = currentSchema.Value.AdditionalProperties.Schema
continue
}
currentSchema = propertySchema
}
Expand All @@ -896,7 +900,11 @@ func makeObject(props map[string]string, schema *openapi3.SchemaRef) (map[string
mapKeys := strings.Split(prop, urlObjectKeyDelimiter)
nestedSchema, err := findNestedSchema(schema, mapKeys)
if err != nil {
return nil, err
path := make([]interface{}, len(mapKeys))
for i, v := range mapKeys {
path[i] = v
}
return nil, &ParseError{path: path, Reason: err.Error()}
}
if nestedSchema.Value.Type == "array" {
path := make([]interface{}, len(mapKeys))
Expand Down
53 changes: 47 additions & 6 deletions openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ func TestDecodeParameter(t *testing.T) {
return s
}

integerSchema = &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "integer"}}
numberSchema = &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "number"}}
booleanSchema = &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "boolean"}}
stringSchema = &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}
allofSchema = &openapi3.SchemaRef{
integerSchema = &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "integer"}}
numberSchema = &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "number"}}
booleanSchema = &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "boolean"}}
stringSchema = &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "string"}}
additionalPropertiesObjectStringSchema = &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "object", AdditionalProperties: openapi3.AdditionalProperties{Schema: stringSchema}}}
additionalPropertiesObjectBoolSchema = &openapi3.SchemaRef{Value: &openapi3.Schema{Type: "object", AdditionalProperties: openapi3.AdditionalProperties{Schema: booleanSchema}}}
allofSchema = &openapi3.SchemaRef{
Value: &openapi3.Schema{
AllOf: []*openapi3.SchemaRef{
integerSchema,
Expand Down Expand Up @@ -670,8 +672,47 @@ func TestDecodeParameter(t *testing.T) {
want: map[string]interface{}{"items": []string{"f%26oo", "bar"}},
found: true,
},
// TODO test additionalProperties works: https://github.com/getkin/kin-openapi/issues/294
// maybe test anyof, oneof...
{
name: "deepObject explode nested object additionalProperties",
param: &openapi3.Parameter{
Name: "param", In: "query", Style: "deepObject", Explode: explode,
Schema: objectOf(
"obj", additionalPropertiesObjectStringSchema,
"objTwo", stringSchema,
),
},
query: "param[obj][prop1]=bar&param[obj][prop2]=foo&param[objTwo]=string",
want: map[string]interface{}{
"obj": map[string]interface{}{"prop1": "bar", "prop2": "foo"},
"objTwo": "string",
},
found: true,
},
{
name: "deepObject explode nested object additionalProperties - bad value",
param: &openapi3.Parameter{
Name: "param", In: "query", Style: "deepObject", Explode: explode,
Schema: objectOf(
"obj", additionalPropertiesObjectBoolSchema,
"objTwo", stringSchema,
),
},
query: "param[obj][prop1]=notbool&param[objTwo]=string",
err: &ParseError{path: []interface{}{"obj"}, Cause: &ParseError{Kind: KindInvalidFormat, Value: "notbool"}},
},
{
name: "deepObject explode nested object additionalProperties - bad index",
param: &openapi3.Parameter{
Name: "param", In: "query", Style: "deepObject", Explode: explode,
Schema: objectOf(
"obj", additionalPropertiesObjectStringSchema,
"objTwo", stringSchema,
),
},
query: "param[obj][prop1]=bar&param[obj][prop2][badindex]=bad&param[objTwo]=string",
err: &ParseError{path: []interface{}{"obj", "prop2", "badindex"}, Reason: `nested schema for key "badindex" not found`},
},
{
name: "deepObject explode nested object",
param: &openapi3.Parameter{
Expand Down

0 comments on commit f7bc1ca

Please sign in to comment.