forked from getkin/kin-openapi
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explicit implementations of un/marshalers
Fixes getkin#513 Gets rid of ./jsoninfo package Signed-off-by: Pierre Fenoll <[email protected]>
- Loading branch information
Showing
6 changed files
with
344 additions
and
17 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
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,132 @@ | ||
package openapi3 | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIssue513OKWithExtension(t *testing.T) { | ||
spec := ` | ||
openapi: "3.0.3" | ||
info: | ||
title: 'My app' | ||
version: 1.0.0 | ||
description: 'An API' | ||
paths: | ||
/v1/operation: | ||
delete: | ||
summary: Delete something | ||
responses: | ||
200: | ||
description: Success | ||
default: | ||
description: '* **400** - Bad Request' | ||
x-my-extension: {val: ue} | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Error' | ||
components: | ||
schemas: | ||
Error: | ||
type: object | ||
description: An error response body. | ||
properties: | ||
message: | ||
description: A detailed message describing the error. | ||
type: string | ||
`[1:] | ||
sl := NewLoader() | ||
doc, err := sl.LoadFromData([]byte(spec)) | ||
require.NoError(t, err) | ||
err = doc.Validate(sl.Context) | ||
require.NoError(t, err) | ||
data, err := json.Marshal(doc) | ||
require.NoError(t, err) | ||
require.Contains(t, string(data), `x-my-extension`) | ||
} | ||
|
||
func TestIssue513KOHasExtraFieldSchema(t *testing.T) { | ||
spec := ` | ||
openapi: "3.0.3" | ||
info: | ||
title: 'My app' | ||
version: 1.0.0 | ||
description: 'An API' | ||
paths: | ||
/v1/operation: | ||
delete: | ||
summary: Delete something | ||
responses: | ||
200: | ||
description: Success | ||
default: | ||
description: '* **400** - Bad Request' | ||
x-my-extension: {val: ue} | ||
# Notice here schema is invalid. It should instead be: | ||
# content: | ||
# application/json: | ||
# schema: | ||
# $ref: '#/components/schemas/Error' | ||
schema: | ||
$ref: '#/components/schemas/Error' | ||
components: | ||
schemas: | ||
Error: | ||
type: object | ||
description: An error response body. | ||
properties: | ||
message: | ||
description: A detailed message describing the error. | ||
type: string | ||
`[1:] | ||
sl := NewLoader() | ||
doc, err := sl.LoadFromData([]byte(spec)) | ||
require.NoError(t, err) | ||
err = doc.Validate(sl.Context) // FIXME unmarshal or validation error | ||
// TODO: merge unmarshal + validation ? | ||
// but still allow Validate so one can modify value then validate without marshaling | ||
require.Error(t, err) | ||
} | ||
|
||
func TestIssue513KOMixesRefAlongWithOtherFields(t *testing.T) { | ||
spec := ` | ||
openapi: "3.0.3" | ||
info: | ||
title: 'My app' | ||
version: 1.0.0 | ||
description: 'An API' | ||
paths: | ||
/v1/operation: | ||
delete: | ||
summary: Delete something | ||
responses: | ||
200: | ||
description: Success | ||
$ref: '#/components/responseBodies/SomeResponseBody' | ||
components: | ||
responseBodies: | ||
SomeResponseBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Error' | ||
schemas: | ||
Error: | ||
type: object | ||
description: An error response body. | ||
properties: | ||
message: | ||
description: A detailed message describing the error. | ||
type: string | ||
`[1:] | ||
sl := NewLoader() | ||
doc, err := sl.LoadFromData([]byte(spec)) | ||
require.Error(t, err) | ||
require.Nil(t, doc) | ||
} |
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.