From 61f00003e98050f18cebc0f56957bef42aec65f8 Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Mon, 4 Dec 2023 19:15:54 +0100 Subject: [PATCH] test(readability): used embedded FS for fixtures Signed-off-by: Frederic BIDON --- circular_test.go | 7 +- expander_test.go | 406 +++-------------------- fixtures/expansion/expectedExtraRef.json | 57 ++++ fixtures/expansion/expectedPathItem.json | 27 ++ fixtures/expansion/petstore2.0.json | 280 ++++++++++++++++ fixtures/specs/mini_spec.json | 18 + fixtures/specs/minimal_spec.json | 44 +++ fixtures/specs/spec.json | 46 +++ swagger_test.go | 156 +++------ 9 files changed, 552 insertions(+), 489 deletions(-) create mode 100644 fixtures/expansion/expectedExtraRef.json create mode 100644 fixtures/expansion/expectedPathItem.json create mode 100644 fixtures/expansion/petstore2.0.json create mode 100644 fixtures/specs/mini_spec.json create mode 100644 fixtures/specs/minimal_spec.json create mode 100644 fixtures/specs/spec.json diff --git a/circular_test.go b/circular_test.go index 6522ce7..cc607c2 100644 --- a/circular_test.go +++ b/circular_test.go @@ -266,7 +266,12 @@ func TestCircular_RemoteExpandAzure(t *testing.T) { require.NotNil(t, pth1) // check expected remaining $ref - assertRefInJSONRegexp(t, jazon, `^(#/definitions/)|(networkInterface.json#/definitions/)|(networkSecurityGroup.json#/definitions/)|(network.json#/definitions)|(virtualNetworkTap.json#/definitions/)|(virtualNetwork.json#/definitions/)|(privateEndpoint.json#/definitions/)|(\./examples/)`) + assertRefInJSONRegexp(t, jazon, + `^(#/definitions/)|(networkInterface.json#/definitions/)|`+ + `(networkSecurityGroup.json#/definitions/)|(network.json#/definitions)|`+ + `(virtualNetworkTap.json#/definitions/)|(virtualNetwork.json#/definitions/)|`+ + `(privateEndpoint.json#/definitions/)|(\./examples/)`, + ) // check all $ref resolve in the expanded root // (filter out the remaining $ref in x-ms-example extensions, which are not expanded) diff --git a/expander_test.go b/expander_test.go index eb7090a..1fc2e5b 100644 --- a/expander_test.go +++ b/expander_test.go @@ -4,7 +4,9 @@ package spec import ( + "embed" "encoding/json" + "fmt" "io" "log" "net/http" @@ -25,12 +27,43 @@ const ( extraRefFixture = "fixtures/expansion/extraRef.json" ) +//nolint:gochecknoglobals // it's okay to have embedded test fixtures as globals var ( + //go:embed fixtures/*/*.json fixtures/*/*.yaml fixtures/*/*.yml + fixtureAssets embed.FS + + // PetStore20 json doc for swagger 2.0 pet store. + PetStore20 []byte + // PetStoreJSONMessage json raw message for Petstore20 - PetStoreJSONMessage = json.RawMessage([]byte(PetStore20)) - specs = filepath.Join("fixtures", "specs") + PetStoreJSONMessage json.RawMessage + expectedExtraRef []byte + expectedPathItem []byte + + specs = filepath.Join("fixtures", "specs") ) +func init() { //nolint:gochecknoinits // it's okay to load embedded fixtures in init(). + // load embedded fixtures + + var err error + PetStore20, err = fixtureAssets.ReadFile("fixtures/expansion/petstore2.0.json") + if err != nil { + panic(fmt.Sprintf("could not find fixture: %v", err)) + } + PetStoreJSONMessage = json.RawMessage(PetStore20) + + expectedExtraRef, err = fixtureAssets.ReadFile("fixtures/expansion/expectedExtraRef.json") + if err != nil { + panic(fmt.Sprintf("could not find fixture: %v", err)) + } + + expectedPathItem, err = fixtureAssets.ReadFile("fixtures/expansion/expectedPathItem.json") + if err != nil { + panic(fmt.Sprintf("could not find fixture: %v", err)) + } +} + func TestExpand_Issue148(t *testing.T) { fp := filepath.Join("fixtures", "bugs", "schema-148.json") b, err := jsonDoc(fp) @@ -997,94 +1030,12 @@ func expandRootWithID(t testing.TB, root *Swagger, testcase string) { func TestExpand_PathItem(t *testing.T) { jazon, _ := expandThisOrDieTrying(t, pathItemsFixture) - assert.JSONEq(t, `{ - "swagger": "2.0", - "info": { - "title": "PathItems refs", - "version": "1.0" - }, - "paths": { - "/todos": { - "get": { - "responses": { - "200": { - "description": "List Todos", - "schema": { - "type": "array", - "items": { - "type": "string" - } - } - }, - "404": { - "description": "error" - } - } - } - } - } - }`, jazon) + assert.JSONEq(t, string(expectedPathItem), jazon) } func TestExpand_ExtraItems(t *testing.T) { jazon, _ := expandThisOrDieTrying(t, extraRefFixture) - assert.JSONEq(t, `{ - "schemes": [ - "http" - ], - "swagger": "2.0", - "info": { - "title": "Supported, but non Swagger 20 compliant $ref constructs", - "version": "2.1.0" - }, - "host": "item.com", - "basePath": "/extraRefs", - "paths": { - "/employees": { - "get": { - "summary": "List Employee Types", - "operationId": "LIST-Employees", - "parameters": [ - { - "description": "unsupported $ref in simple param", - "type": "array", - "items": { - "$ref": "#/definitions/arrayType" - }, - "name": "myQueryParam", - "in": "query" - } - ], - "responses": { - "200": { - "description": "unsupported $ref in header", - "schema": { - "type": "string" - }, - "headers": { - "X-header": { - "type": "array", - "items": { - "$ref": "#/definitions/headerType" - } - } - } - } - } - } - } - }, - "definitions": { - "arrayType": { - "type": "integer", - "format": "int32" - }, - "headerType": { - "type": "string", - "format": "uuid" - } - } - }`, jazon) + assert.JSONEq(t, string(expectedExtraRef), jazon) } func TestExpand_Issue145(t *testing.T) { @@ -1133,286 +1084,3 @@ func TestExpand_Issue145(t *testing.T) { }) }) } - -// PetStore20 json doc for swagger 2.0 pet store -const PetStore20 = `{ - "swagger": "2.0", - "info": { - "version": "1.0.0", - "title": "Swagger Petstore", - "contact": { - "name": "Wordnik API Team", - "url": "http://developer.wordnik.com" - }, - "license": { - "name": "Creative Commons 4.0 International", - "url": "http://creativecommons.org/licenses/by/4.0/" - } - }, - "host": "petstore.swagger.wordnik.com", - "basePath": "/api", - "schemes": [ - "http" - ], - "paths": { - "/pets": { - "get": { - "security": [ - { - "basic": [] - } - ], - "tags": [ "Pet Operations" ], - "operationId": "getAllPets", - "parameters": [ - { - "name": "status", - "in": "query", - "description": "The status to filter by", - "type": "string" - }, - { - "name": "limit", - "in": "query", - "description": "The maximum number of results to return", - "type": "integer", - "format": "int64" - } - ], - "summary": "Finds all pets in the system", - "responses": { - "200": { - "description": "Pet response", - "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Pet" - } - } - }, - "default": { - "description": "Unexpected error", - "schema": { - "$ref": "#/definitions/Error" - } - } - } - }, - "post": { - "security": [ - { - "basic": [] - } - ], - "tags": [ "Pet Operations" ], - "operationId": "createPet", - "summary": "Creates a new pet", - "consumes": ["application/x-yaml"], - "produces": ["application/x-yaml"], - "parameters": [ - { - "name": "pet", - "in": "body", - "description": "The Pet to create", - "required": true, - "schema": { - "$ref": "#/definitions/newPet" - } - } - ], - "responses": { - "200": { - "description": "Created Pet response", - "schema": { - "$ref": "#/definitions/Pet" - } - }, - "default": { - "description": "Unexpected error", - "schema": { - "$ref": "#/definitions/Error" - } - } - } - } - }, - "/pets/{id}": { - "delete": { - "security": [ - { - "apiKey": [] - } - ], - "description": "Deletes the Pet by id", - "operationId": "deletePet", - "parameters": [ - { - "name": "id", - "in": "path", - "description": "ID of pet to delete", - "required": true, - "type": "integer", - "format": "int64" - } - ], - "responses": { - "204": { - "description": "pet deleted" - }, - "default": { - "description": "unexpected error", - "schema": { - "$ref": "#/definitions/Error" - } - } - } - }, - "get": { - "tags": [ "Pet Operations" ], - "operationId": "getPetById", - "summary": "Finds the pet by id", - "responses": { - "200": { - "description": "Pet response", - "schema": { - "$ref": "#/definitions/Pet" - } - }, - "default": { - "description": "Unexpected error", - "schema": { - "$ref": "#/definitions/Error" - } - } - } - }, - "parameters": [ - { - "name": "id", - "in": "path", - "description": "ID of pet", - "required": true, - "type": "integer", - "format": "int64" - } - ] - } - }, - "definitions": { - "Category": { - "id": "Category", - "properties": { - "id": { - "format": "int64", - "type": "integer" - }, - "name": { - "type": "string" - } - } - }, - "Pet": { - "id": "Pet", - "properties": { - "category": { - "$ref": "#/definitions/Category" - }, - "id": { - "description": "unique identifier for the pet", - "format": "int64", - "maximum": 100.0, - "minimum": 0.0, - "type": "integer" - }, - "name": { - "type": "string" - }, - "photoUrls": { - "items": { - "type": "string" - }, - "type": "array" - }, - "status": { - "description": "pet status in the store", - "enum": [ - "available", - "pending", - "sold" - ], - "type": "string" - }, - "tags": { - "items": { - "$ref": "#/definitions/Tag" - }, - "type": "array" - } - }, - "required": [ - "id", - "name" - ] - }, - "newPet": { - "anyOf": [ - { - "$ref": "#/definitions/Pet" - }, - { - "required": [ - "name" - ] - } - ] - }, - "Tag": { - "id": "Tag", - "properties": { - "id": { - "format": "int64", - "type": "integer" - }, - "name": { - "type": "string" - } - } - }, - "Error": { - "required": [ - "code", - "message" - ], - "properties": { - "code": { - "type": "integer", - "format": "int32" - }, - "message": { - "type": "string" - } - } - } - }, - "consumes": [ - "application/json", - "application/xml" - ], - "produces": [ - "application/json", - "application/xml", - "text/plain", - "text/html" - ], - "securityDefinitions": { - "basic": { - "type": "basic" - }, - "apiKey": { - "type": "apiKey", - "in": "header", - "name": "X-API-KEY" - } - } -} -` diff --git a/fixtures/expansion/expectedExtraRef.json b/fixtures/expansion/expectedExtraRef.json new file mode 100644 index 0000000..24da091 --- /dev/null +++ b/fixtures/expansion/expectedExtraRef.json @@ -0,0 +1,57 @@ +{ + "schemes": [ + "http" + ], + "swagger": "2.0", + "info": { + "title": "Supported, but non Swagger 20 compliant $ref constructs", + "version": "2.1.0" + }, + "host": "item.com", + "basePath": "/extraRefs", + "paths": { + "/employees": { + "get": { + "summary": "List Employee Types", + "operationId": "LIST-Employees", + "parameters": [ + { + "description": "unsupported $ref in simple param", + "type": "array", + "items": { + "$ref": "#/definitions/arrayType" + }, + "name": "myQueryParam", + "in": "query" + } + ], + "responses": { + "200": { + "description": "unsupported $ref in header", + "schema": { + "type": "string" + }, + "headers": { + "X-header": { + "type": "array", + "items": { + "$ref": "#/definitions/headerType" + } + } + } + } + } + } + } + }, + "definitions": { + "arrayType": { + "type": "integer", + "format": "int32" + }, + "headerType": { + "type": "string", + "format": "uuid" + } + } +} diff --git a/fixtures/expansion/expectedPathItem.json b/fixtures/expansion/expectedPathItem.json new file mode 100644 index 0000000..6eed8b6 --- /dev/null +++ b/fixtures/expansion/expectedPathItem.json @@ -0,0 +1,27 @@ +{ + "swagger": "2.0", + "info": { + "title": "PathItems refs", + "version": "1.0" + }, + "paths": { + "/todos": { + "get": { + "responses": { + "200": { + "description": "List Todos", + "schema": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "404": { + "description": "error" + } + } + } + } + } +} diff --git a/fixtures/expansion/petstore2.0.json b/fixtures/expansion/petstore2.0.json new file mode 100644 index 0000000..7fc9d88 --- /dev/null +++ b/fixtures/expansion/petstore2.0.json @@ -0,0 +1,280 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Swagger Petstore", + "contact": { + "name": "Wordnik API Team", + "url": "http://developer.wordnik.com" + }, + "license": { + "name": "Creative Commons 4.0 International", + "url": "http://creativecommons.org/licenses/by/4.0/" + } + }, + "host": "petstore.swagger.wordnik.com", + "basePath": "/api", + "schemes": [ + "http" + ], + "paths": { + "/pets": { + "get": { + "security": [ + { + "basic": [] + } + ], + "tags": [ "Pet Operations" ], + "operationId": "getAllPets", + "parameters": [ + { + "name": "status", + "in": "query", + "description": "The status to filter by", + "type": "string" + }, + { + "name": "limit", + "in": "query", + "description": "The maximum number of results to return", + "type": "integer", + "format": "int64" + } + ], + "summary": "Finds all pets in the system", + "responses": { + "200": { + "description": "Pet response", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Pet" + } + } + }, + "default": { + "description": "Unexpected error", + "schema": { + "$ref": "#/definitions/Error" + } + } + } + }, + "post": { + "security": [ + { + "basic": [] + } + ], + "tags": [ "Pet Operations" ], + "operationId": "createPet", + "summary": "Creates a new pet", + "consumes": ["application/x-yaml"], + "produces": ["application/x-yaml"], + "parameters": [ + { + "name": "pet", + "in": "body", + "description": "The Pet to create", + "required": true, + "schema": { + "$ref": "#/definitions/newPet" + } + } + ], + "responses": { + "200": { + "description": "Created Pet response", + "schema": { + "$ref": "#/definitions/Pet" + } + }, + "default": { + "description": "Unexpected error", + "schema": { + "$ref": "#/definitions/Error" + } + } + } + } + }, + "/pets/{id}": { + "delete": { + "security": [ + { + "apiKey": [] + } + ], + "description": "Deletes the Pet by id", + "operationId": "deletePet", + "parameters": [ + { + "name": "id", + "in": "path", + "description": "ID of pet to delete", + "required": true, + "type": "integer", + "format": "int64" + } + ], + "responses": { + "204": { + "description": "pet deleted" + }, + "default": { + "description": "unexpected error", + "schema": { + "$ref": "#/definitions/Error" + } + } + } + }, + "get": { + "tags": [ "Pet Operations" ], + "operationId": "getPetById", + "summary": "Finds the pet by id", + "responses": { + "200": { + "description": "Pet response", + "schema": { + "$ref": "#/definitions/Pet" + } + }, + "default": { + "description": "Unexpected error", + "schema": { + "$ref": "#/definitions/Error" + } + } + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "description": "ID of pet", + "required": true, + "type": "integer", + "format": "int64" + } + ] + } + }, + "definitions": { + "Category": { + "id": "Category", + "properties": { + "id": { + "format": "int64", + "type": "integer" + }, + "name": { + "type": "string" + } + } + }, + "Pet": { + "id": "Pet", + "properties": { + "category": { + "$ref": "#/definitions/Category" + }, + "id": { + "description": "unique identifier for the pet", + "format": "int64", + "maximum": 100.0, + "minimum": 0.0, + "type": "integer" + }, + "name": { + "type": "string" + }, + "photoUrls": { + "items": { + "type": "string" + }, + "type": "array" + }, + "status": { + "description": "pet status in the store", + "enum": [ + "available", + "pending", + "sold" + ], + "type": "string" + }, + "tags": { + "items": { + "$ref": "#/definitions/Tag" + }, + "type": "array" + } + }, + "required": [ + "id", + "name" + ] + }, + "newPet": { + "anyOf": [ + { + "$ref": "#/definitions/Pet" + }, + { + "required": [ + "name" + ] + } + ] + }, + "Tag": { + "id": "Tag", + "properties": { + "id": { + "format": "int64", + "type": "integer" + }, + "name": { + "type": "string" + } + } + }, + "Error": { + "required": [ + "code", + "message" + ], + "properties": { + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + } + } + } + }, + "consumes": [ + "application/json", + "application/xml" + ], + "produces": [ + "application/json", + "application/xml", + "text/plain", + "text/html" + ], + "securityDefinitions": { + "basic": { + "type": "basic" + }, + "apiKey": { + "type": "apiKey", + "in": "header", + "name": "X-API-KEY" + } + } +} diff --git a/fixtures/specs/mini_spec.json b/fixtures/specs/mini_spec.json new file mode 100644 index 0000000..e153eaf --- /dev/null +++ b/fixtures/specs/mini_spec.json @@ -0,0 +1,18 @@ +{ + "swagger": "2.0", + "info": { + "version": "0.0.0", + "title": "Simple API" + }, + "paths": { + "/": { + "get": { + "responses": { + "200": { + "description": "OK" + } + } + } + } + } +} diff --git a/fixtures/specs/minimal_spec.json b/fixtures/specs/minimal_spec.json new file mode 100644 index 0000000..c7c2f7f --- /dev/null +++ b/fixtures/specs/minimal_spec.json @@ -0,0 +1,44 @@ +{ + "swagger": "2.0", + "info": { + "version": "0.0.0", + "title": "Simple API" + }, + "securityDefinitions": { + "basic": { + "type": "basic" + }, + "apiKey": { + "type": "apiKey", + "in": "header", + "name": "X-API-KEY" + }, + "queryKey": { + "type": "apiKey", + "in": "query", + "name": "api_key" + } + }, + "paths": { + "/": { + "get": { + "security": [ + { + "apiKey": [], + "basic": [] + }, + {}, + { + "queryKey": [], + "basic": [] + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + } + } +} diff --git a/fixtures/specs/spec.json b/fixtures/specs/spec.json new file mode 100644 index 0000000..0b65bcc --- /dev/null +++ b/fixtures/specs/spec.json @@ -0,0 +1,46 @@ +{ + "id": "http://localhost:3849/api-docs", + "consumes": ["application/json", "application/x-yaml"], + "produces": ["application/json"], + "schemes": ["http", "https"], + "swagger": "2.0", + "info": { + "contact": { + "name": "wordnik api team", + "url": "http://developer.wordnik.com" + }, + "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification", + "license": { + "name": "Creative Commons 4.0 International", + "url": "http://creativecommons.org/licenses/by/4.0/" + }, + "termsOfService": "http://helloreverb.com/terms/", + "title": "Swagger Sample API", + "version": "1.0.9-abcd", + "x-framework": "go-swagger" + }, + "host": "some.api.out.there", + "basePath": "/", + "paths": {"x-framework":"go-swagger","/":{"$ref":"cats"}}, + "definitions": { "Category": { "type": "string"} }, + "parameters": { + "categoryParam": { + "name": "category", + "in": "query", + "type": "string" + } + }, + "responses": { "EmptyAnswer": { "description": "no data to return for this operation" } }, + "securityDefinitions": { + "internalApiKey": { + "type": "apiKey", + "in": "header", + "name": "api_key" + } + }, + "security": [{"internalApiKey":[]}], + "tags": [{"name":"pets"}], + "externalDocs": {"description":"the name","url":"the url"}, + "x-some-extension": "vendor", + "x-schemes": ["unix","amqp"] +} diff --git a/swagger_test.go b/swagger_test.go index 4aad391..5c16740 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -5,12 +5,40 @@ package spec import ( "encoding/json" + "fmt" "testing" "github.com/go-openapi/testify/v2/assert" "github.com/go-openapi/testify/v2/require" ) +//nolint:gochecknoglobals // it's okay to have embedded test fixtures as globals +var ( + specJSON []byte + minimalJSONSpec []byte + miniJSONSpec []byte +) + +func init() { //nolint:gochecknoinits // it's okay to load embedded fixtures in init(). + // load embedded fixtures + + var err error + specJSON, err = fixtureAssets.ReadFile("fixtures/specs/spec.json") + if err != nil { + panic(fmt.Sprintf("could not find fixture: %v", err)) + } + + minimalJSONSpec, err = fixtureAssets.ReadFile("fixtures/specs/minimal_spec.json") + if err != nil { + panic(fmt.Sprintf("could not find fixture: %v", err)) + } + + miniJSONSpec, err = fixtureAssets.ReadFile("fixtures/specs/mini_spec.json") + if err != nil { + panic(fmt.Sprintf("could not find fixture: %v", err)) + } +} + var spec = Swagger{ SwaggerProps: SwaggerProps{ ID: "http://localhost:3849/api-docs", @@ -48,54 +76,6 @@ var spec = Swagger{ }}, } -const specJSON = `{ - "id": "http://localhost:3849/api-docs", - "consumes": ["application/json", "application/x-yaml"], - "produces": ["application/json"], - "schemes": ["http", "https"], - "swagger": "2.0", - "info": { - "contact": { - "name": "wordnik api team", - "url": "http://developer.wordnik.com" - }, - "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0` + - ` specification", - "license": { - "name": "Creative Commons 4.0 International", - "url": "http://creativecommons.org/licenses/by/4.0/" - }, - "termsOfService": "http://helloreverb.com/terms/", - "title": "Swagger Sample API", - "version": "1.0.9-abcd", - "x-framework": "go-swagger" - }, - "host": "some.api.out.there", - "basePath": "/", - "paths": {"x-framework":"go-swagger","/":{"$ref":"cats"}}, - "definitions": { "Category": { "type": "string"} }, - "parameters": { - "categoryParam": { - "name": "category", - "in": "query", - "type": "string" - } - }, - "responses": { "EmptyAnswer": { "description": "no data to return for this operation" } }, - "securityDefinitions": { - "internalApiKey": { - "type": "apiKey", - "in": "header", - "name": "api_key" - } - }, - "security": [{"internalApiKey":[]}], - "tags": [{"name":"pets"}], - "externalDocs": {"description":"the name","url":"the url"}, - "x-some-extension": "vendor", - "x-schemes": ["unix","amqp"] -}` - // func verifySpecSerialize(specJSON []byte, spec Swagger) { // expected := map[string]interface{}{} // json.Unmarshal(specJSON, &expected) @@ -227,7 +207,7 @@ func assertSpecJSON(t testing.TB, specJSON []byte) bool { func TestSwaggerSpec_Serialize(t *testing.T) { expected := make(map[string]any) - _ = json.Unmarshal([]byte(specJSON), &expected) + _ = json.Unmarshal(specJSON, &expected) b, err := json.MarshalIndent(spec, "", " ") require.NoError(t, err) var actual map[string]any @@ -237,13 +217,13 @@ func TestSwaggerSpec_Serialize(t *testing.T) { func TestSwaggerSpec_Deserialize(t *testing.T) { var actual Swagger - require.NoError(t, json.Unmarshal([]byte(specJSON), &actual)) + require.NoError(t, json.Unmarshal(specJSON, &actual)) assert.Equal(t, actual, spec) } func TestVendorExtensionStringSlice(t *testing.T) { var actual Swagger - require.NoError(t, json.Unmarshal([]byte(specJSON), &actual)) + require.NoError(t, json.Unmarshal(specJSON, &actual)) schemes, ok := actual.Extensions.GetStringSlice("x-schemes") require.True(t, ok) assert.Equal(t, []string{"unix", "amqp"}, schemes) @@ -267,27 +247,8 @@ func TestVendorExtensionStringSlice(t *testing.T) { } func TestOptionalSwaggerProps_Serialize(t *testing.T) { - minimalJSONSpec := []byte(`{ - "swagger": "2.0", - "info": { - "version": "0.0.0", - "title": "Simple API" - }, - "paths": { - "/": { - "get": { - "responses": { - "200": { - "description": "OK" - } - } - } - } - } -}`) - var minimalSpec Swagger - err := json.Unmarshal(minimalJSONSpec, &minimalSpec) + err := json.Unmarshal(miniJSONSpec, &minimalSpec) require.NoError(t, err) bytes, err := json.Marshal(&minimalSpec) require.NoError(t, err) @@ -309,51 +270,6 @@ func TestOptionalSwaggerProps_Serialize(t *testing.T) { assert.NotContains(t, ms, "externalDocs") } -var minimalJSONSpec = []byte(`{ - "swagger": "2.0", - "info": { - "version": "0.0.0", - "title": "Simple API" - }, - "securityDefinitions": { - "basic": { - "type": "basic" - }, - "apiKey": { - "type": "apiKey", - "in": "header", - "name": "X-API-KEY" - }, - "queryKey": { - "type": "apiKey", - "in": "query", - "name": "api_key" - } - }, - "paths": { - "/": { - "get": { - "security": [ - { - "apiKey": [], - "basic": [] - }, - {}, - { - "queryKey": [], - "basic": [] - } - ], - "responses": { - "200": { - "description": "OK" - } - } - } - } - } - }`) - func TestSecurityRequirements(t *testing.T) { var minimalSpec Swagger require.NoError(t, json.Unmarshal(minimalJSONSpec, &minimalSpec)) @@ -370,12 +286,14 @@ func TestSecurityRequirements(t *testing.T) { func TestSwaggerGobEncoding(t *testing.T) { doTestSwaggerGobEncoding(t, specJSON) - doTestSwaggerGobEncoding(t, string(minimalJSONSpec)) + doTestSwaggerGobEncoding(t, minimalJSONSpec) } -func doTestSwaggerGobEncoding(t *testing.T, fixture string) { +func doTestSwaggerGobEncoding(t *testing.T, fixture []byte) { + t.Helper() + var src, dst Swagger - require.NoError(t, json.Unmarshal([]byte(fixture), &src)) + require.NoError(t, json.Unmarshal(fixture, &src)) doTestAnyGobEncoding(t, &src, &dst) }