Skip to content

Commit

Permalink
Follow up to #540 with more tests (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp committed May 30, 2022
1 parent 770fcc5 commit 121fc06
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 7 deletions.
24 changes: 17 additions & 7 deletions openapi3/internalize_refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package openapi3

import (
"context"
"io/ioutil"
"regexp"
"testing"

"github.com/stretchr/testify/require"
)

func TestInternalizeRefs(t *testing.T) {
var regexpRef = regexp.MustCompile(`"\$ref":`)
var regexpRefInternal = regexp.MustCompile(`"\$ref":"\#`)
ctx := context.Background()

regexpRef := regexp.MustCompile(`"\$ref":`)
regexpRefInternal := regexp.MustCompile(`"\$ref":"#`)

tests := []struct {
filename string
Expand All @@ -28,13 +31,15 @@ func TestInternalizeRefs(t *testing.T) {
sl.IsExternalRefsAllowed = true
doc, err := sl.LoadFromFile(test.filename)
require.NoError(t, err, "loading test file")
err = doc.Validate(ctx)
require.NoError(t, err, "validating spec")

// Internalize the references
doc.InternalizeRefs(context.Background(), DefaultRefNameResolver)
doc.InternalizeRefs(ctx, nil)

// Validate the internalized spec
err = doc.Validate(context.Background())
require.Nil(t, err, "validating internalized spec")
err = doc.Validate(ctx)
require.NoError(t, err, "validating internalized spec")

data, err := doc.MarshalJSON()
require.NoError(t, err, "marshalling internalized spec")
Expand All @@ -48,8 +53,13 @@ func TestInternalizeRefs(t *testing.T) {
// load from data, but with the path set to the current directory
doc2, err := sl.LoadFromData(data)
require.NoError(t, err, "reloading spec")
err = doc2.Validate(context.Background())
require.Nil(t, err, "validating reloaded spec")
err = doc2.Validate(ctx)
require.NoError(t, err, "validating reloaded spec")

// compare with expected
data0, err := ioutil.ReadFile(test.filename + ".internalized.yml")
require.NoError(t, err)
require.JSONEq(t, string(data), string(data0))
})
}
}
131 changes: 131 additions & 0 deletions openapi3/testdata/callbacks.yml.internalized.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"components": {
"callbacks": {
"MyCallbackEvent": {
"{$request.query.queryUrl}": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SomeOtherPayload"
}
}
},
"description": "Callback payload"
},
"responses": {
"200": {
"description": "callback successfully processed"
}
}
}
}
}
},
"schemas": {
"SomeOtherPayload": {
"type": "boolean"
},
"SomePayload": {
"type": "object"
}
}
},
"info": {
"title": "Callback refd",
"version": "1.2.3"
},
"openapi": "3.1.0",
"paths": {
"/other": {
"post": {
"callbacks": {
"myEvent": {
"$ref": "#/components/callbacks/MyCallbackEvent"
}
},
"parameters": [
{
"description": "bla\nbla\nbla\n",
"in": "query",
"name": "queryUrl",
"required": true,
"schema": {
"example": "https://example.com",
"format": "uri",
"type": "string"
}
}
],
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
},
"description": ""
}
}
}
},
"/trans": {
"post": {
"callbacks": {
"transactionCallback": {
"http://notificationServer.com?transactionId={$request.body#/id}&email={$request.body#/email}": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SomePayload"
}
}
},
"description": "Callback payload"
},
"responses": {
"200": {
"description": "callback successfully processed"
}
}
}
}
}
},
"requestBody": {
"content": {
"application/json": {
"schema": {
"properties": {
"email": {
"format": "email"
},
"id": {
"type": "string"
}
}
}
}
}
},
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
},
"description": "subscription successfully created"
}
}
}
}
}
}
68 changes: 68 additions & 0 deletions openapi3/testdata/recursiveRef/openapi.yml.internalized.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"components": {
"parameters": {
"number": {
"in": "query",
"name": "someNumber",
"schema": {
"type": "string"
}
}
},
"schemas": {
"Bar": {
"example": "bar",
"type": "string"
},
"Foo": {
"properties": {
"bar": {
"$ref": "#/components/schemas/Bar"
}
},
"type": "object"
},
"Foo2": {
"properties": {
"foo": {
"$ref": "#/components/schemas/Foo"
}
},
"type": "object"
}
}
},
"info": {
"title": "Recursive refs example",
"version": "1.0"
},
"openapi": "3.0.3",
"paths": {
"/foo": {
"get": {
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"properties": {
"foo2": {
"$ref": "#/components/schemas/Foo2"
}
},
"type": "object"
}
}
},
"description": "OK"
}
}
},
"parameters": [
{
"$ref": "#/components/parameters/number"
}
]
}
}
}
36 changes: 36 additions & 0 deletions openapi3/testdata/spec.yaml.internalized.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"components": {
"schemas": {
"Test": {
"properties": {
"test": {
"$ref": "#/components/schemas/b"
}
},
"type": "object"
},
"a": {
"type": "string"
},
"b": {
"description": "I use a local reference.",
"properties": {
"name": {
"$ref": "#/components/schemas/a"
}
},
"type": "object"
}
}
},
"info": {
"license": {
"name": "MIT"
},
"title": "Some Swagger",
"version": "1.0.0"
},
"openapi": "3.0.1",
"paths": {
}
}
19 changes: 19 additions & 0 deletions openapi3/testdata/testref.openapi.yml.internalized.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"components": {
"schemas": {
"AnotherTestSchema": {
"type": "string"
},
"CustomTestSchema": {
"type": "string"
}
}
},
"info": {
"title": "OAI Specification w/ refs in YAML",
"version": "1"
},
"openapi": "3.0.0",
"paths": {
}
}

0 comments on commit 121fc06

Please sign in to comment.