-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
- Loading branch information
Showing
34 changed files
with
523 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package openapi3 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIssue495(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: '#' | ||
paths: | ||
/categories: | ||
get: | ||
responses: | ||
'200': | ||
description: '' | ||
content: | ||
application/json: | ||
schema: | ||
properties: | ||
allOf: | ||
$ref: '#/components/schemas/schemaArray' | ||
`[1:]) | ||
|
||
sl := NewLoader() | ||
|
||
doc, err := sl.LoadFromData(spec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(sl.Context) | ||
require.EqualError(t, err, `invalid components: schema "schemaArray": found unresolved ref: "#"`) | ||
} | ||
|
||
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: | ||
properties: | ||
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) | ||
|
||
require.Equal(t, &Schema{Type: "object"}, doc.Components.Schemas["schemaArray"].Value.Items.Value) | ||
} | ||
|
||
func TestIssue495WithDraft04(t *testing.T) { | ||
spec := []byte(` | ||
openapi: 3.0.1 | ||
servers: | ||
- url: http://localhost:5000 | ||
info: | ||
version: v1 | ||
title: Products api | ||
contact: | ||
name: me | ||
email: me@github.com | ||
description: This is a sample | ||
paths: | ||
/categories: | ||
get: | ||
summary: Provides the available categories for the store | ||
operationId: list-categories | ||
responses: | ||
'200': | ||
description: this is a desc | ||
content: | ||
application/json: | ||
schema: | ||
$ref: http://json-schema.org/draft-04/schema | ||
`[1:]) | ||
|
||
sl := NewLoader() | ||
sl.IsExternalRefsAllowed = true | ||
|
||
doc, err := sl.LoadFromData(spec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(sl.Context) | ||
require.ErrorContains(t, err, `found unresolved ref: "#"`) | ||
} | ||
|
||
func TestIssue495WithDraft04Bis(t *testing.T) { | ||
spec := []byte(` | ||
openapi: 3.0.1 | ||
servers: | ||
- url: http://localhost:5000 | ||
info: | ||
version: v1 | ||
title: Products api | ||
contact: | ||
name: me | ||
email: me@github.com | ||
description: This is a sample | ||
paths: | ||
/categories: | ||
get: | ||
summary: Provides the available categories for the store | ||
operationId: list-categories | ||
responses: | ||
'200': | ||
description: this is a desc | ||
content: | ||
application/json: | ||
schema: | ||
$ref: testdata/draft04.yml | ||
`[1:]) | ||
|
||
sl := NewLoader() | ||
sl.IsExternalRefsAllowed = true | ||
|
||
doc, err := sl.LoadFromData(spec) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(sl.Context) | ||
require.ErrorContains(t, err, `found unresolved ref: "#"`) | ||
} |
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.