-
-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
openapi{2,3}: simplify unmarshal errors (#870)
- Loading branch information
Showing
32 changed files
with
222 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package openapi2 | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/invopop/yaml" | ||
) | ||
|
||
func unmarshalError(jsonUnmarshalErr error) error { | ||
if before, after, found := strings.Cut(jsonUnmarshalErr.Error(), "Bis."); found && before != "" && after != "" { | ||
before = strings.ReplaceAll(before, " Go struct ", " ") | ||
return fmt.Errorf("%s.%s", before, after) | ||
} | ||
return jsonUnmarshalErr | ||
} | ||
|
||
func unmarshal(data []byte, v interface{}) error { | ||
// See https://github.com/getkin/kin-openapi/issues/680 | ||
if err := json.Unmarshal(data, v); err != nil { | ||
// UnmarshalStrict(data, v) TODO: investigate how ymlv3 handles duplicate map keys | ||
return yaml.Unmarshal(data, v) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package openapi2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnmarshalError(t *testing.T) { | ||
{ | ||
v2 := []byte(` | ||
openapi: '2.0' | ||
info: | ||
version: '1.10' | ||
title: title | ||
paths: | ||
"/ping": | ||
post: | ||
consumes: | ||
- multipart/form-data | ||
parameters: | ||
name: file # <-- Missing dash | ||
in: formData | ||
description: file | ||
required: true | ||
type: file | ||
responses: | ||
'200': | ||
description: OK | ||
`[1:]) | ||
|
||
var doc T | ||
err := unmarshal(v2, &doc) | ||
require.ErrorContains(t, err, `json: cannot unmarshal object into field Operation.parameters of type openapi2.Parameters`) | ||
} | ||
|
||
v2 := []byte(` | ||
openapi: '2.0' | ||
info: | ||
version: '1.10' | ||
title: title | ||
paths: | ||
"/ping": | ||
post: | ||
consumes: | ||
- multipart/form-data | ||
parameters: | ||
- name: file # <-- | ||
in: formData | ||
description: file | ||
required: true | ||
type: file | ||
responses: | ||
'200': | ||
description: OK | ||
`[1:]) | ||
|
||
var doc T | ||
err := unmarshal(v2, &doc) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package openapi3 | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/invopop/yaml" | ||
) | ||
|
||
func unmarshalError(jsonUnmarshalErr error) error { | ||
if before, after, found := strings.Cut(jsonUnmarshalErr.Error(), "Bis."); found && before != "" && after != "" { | ||
before = strings.ReplaceAll(before, " Go struct ", " ") | ||
return fmt.Errorf("%s.%s", before, after) | ||
} | ||
return jsonUnmarshalErr | ||
} | ||
|
||
func unmarshal(data []byte, v interface{}) error { | ||
// See https://github.com/getkin/kin-openapi/issues/680 | ||
if err := json.Unmarshal(data, v); err != nil { | ||
// UnmarshalStrict(data, v) TODO: investigate how ymlv3 handles duplicate map keys | ||
return yaml.Unmarshal(data, v) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package openapi3 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnmarshalError(t *testing.T) { | ||
{ | ||
spec := []byte(` | ||
openapi: 3.0.1 | ||
info: | ||
version: v1 | ||
title: Products api | ||
components: | ||
schemas: | ||
someSchema: | ||
type: object | ||
schemaArray: | ||
type: array | ||
minItems: 1 | ||
items: | ||
$ref: '#/components/schemas/someSchema' | ||
paths: | ||
/categories: | ||
get: | ||
responses: | ||
'200': | ||
description: '' | ||
content: | ||
application/json: | ||
schema: | ||
allOf: | ||
$ref: '#/components/schemas/schemaArray' # <- Should have been a list | ||
`[1:]) | ||
|
||
sl := NewLoader() | ||
|
||
_, err := sl.LoadFromData(spec) | ||
require.ErrorContains(t, err, `json: cannot unmarshal object into field Schema.allOf of type openapi3.SchemaRefs`) | ||
} | ||
|
||
spec := []byte(` | ||
openapi: 3.0.1 | ||
info: | ||
version: v1 | ||
title: Products api | ||
components: | ||
schemas: | ||
someSchema: | ||
type: object | ||
schemaArray: | ||
type: array | ||
minItems: 1 | ||
items: | ||
$ref: '#/components/schemas/someSchema' | ||
paths: | ||
/categories: | ||
get: | ||
responses: | ||
'200': | ||
description: '' | ||
content: | ||
application/json: | ||
schema: | ||
allOf: | ||
- $ref: '#/components/schemas/schemaArray' # <- | ||
`[1:]) | ||
|
||
sl := NewLoader() | ||
|
||
doc, err := sl.LoadFromData(spec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(sl.Context) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.