From ecb06bc5c05b010b9c1322486ff74f2178f7927c Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Thu, 2 Feb 2023 15:35:40 +0100 Subject: [PATCH] openapi3: add test from #731 showing validating doc first is required (#762) closes https://github.com/getkin/kin-openapi/issues/731 --- openapi3/loader_test.go | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/openapi3/loader_test.go b/openapi3/loader_test.go index 3515586a0..9756f55fc 100644 --- a/openapi3/loader_test.go +++ b/openapi3/loader_test.go @@ -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))