Skip to content

Commit

Permalink
openapi3: add test from #731 showing validating doc first is required (
Browse files Browse the repository at this point in the history
…#762)

closes #731
  • Loading branch information
fenollp authored Feb 2, 2023
1 parent 409e0dc commit ecb06bc
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions openapi3/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,53 @@ paths:
`)

loader := NewLoader()

doc, err := loader.LoadFromData(spec)
require.NoError(t, err)
require.Equal(t, "An API", doc.Info.Title)
require.Equal(t, 2, len(doc.Components.Schemas))
require.Equal(t, 1, len(doc.Paths))
def := doc.Paths["/items"].Put.Responses.Default().Value
desc := "unexpected error"
require.Equal(t, &desc, def.Description)
require.Equal(t, "unexpected error", *doc.Paths["/items"].Put.Responses.Default().Value.Description)

err = doc.Validate(loader.Context)
require.NoError(t, err)
}

func TestIssue731(t *testing.T) {
spec := []byte(`
openapi: 3.0.0
info:
title: An API
version: v1
paths:
/items:
put:
description: ''
requestBody:
required: true
# Note mis-indented content block
content:
application/json:
schema:
type: object
responses:
default:
description: unexpected error
content:
application/json:
schema:
type: object
`[1:])

loader := NewLoader()

doc, err := loader.LoadFromData(spec)
require.NoError(t, err)

err = doc.Validate(loader.Context)
require.ErrorContains(t, err, `content of the request body is required`)
}

func ExampleLoader() {
const source = `{"info":{"description":"An API"}}`
doc, err := NewLoader().LoadFromData([]byte(source))
Expand Down

0 comments on commit ecb06bc

Please sign in to comment.