$52 GRAYBYTE WORDPRESS FILE MANAGER $82

SERVER : in-mum-web1330.main-hosting.eu #1 SMP Mon Feb 10 22:45:17 UTC 2025
SERVER IP : 91.108.106.67 | ADMIN IP 216.73.216.215
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/go/pkg/mod/github.com/go-openapi/spec@v0.20.8/

HOME
Current File : /opt/go/pkg/mod/github.com/go-openapi/spec@v0.20.8//circular_test.go
package spec

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"path/filepath"
	"testing"
	"time"

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

func TestExpandCircular_Issue3(t *testing.T) {
	jazon, root := expandThisOrDieTrying(t, "fixtures/expansion/overflow.json")
	require.NotEmpty(t, jazon)

	// circular $ref point to the expanded root document
	assertRefInJSON(t, jazon, "#/definitions")

	// verify that all $ref can resolved against the new root schema
	assertRefResolve(t, jazon, "", root)

	// verify that all $ref can be expanded in the new root schema
	assertRefExpand(t, jazon, "", root)
}

func TestExpandCircular_RefExpansion(t *testing.T) {
	basePath := filepath.Join("fixtures", "expansion", "circularRefs.json")

	carsDoc, err := jsonDoc(basePath)
	require.NoError(t, err)

	spec := new(Swagger)
	require.NoError(t, json.Unmarshal(carsDoc, spec))

	resolver := defaultSchemaLoader(spec, &ExpandOptions{RelativeBase: basePath}, nil, nil)

	schema := spec.Definitions["car"]

	_, err = expandSchema(schema, []string{"#/definitions/car"}, resolver, normalizeBase(basePath))
	require.NoError(t, err)

	jazon := asJSON(t, schema)

	// circular $ref point to the expanded root document
	// there are only 2 types with circular definitions
	assertRefInJSONRegexp(t, jazon, "#/definitions/(car|category)")

	// verify that all $ref can resolved against the new root schema
	assertRefResolve(t, jazon, "", spec)

	// verify that all $ref can be expanded in the new root schema
	assertRefExpand(t, jazon, "", spec)
}

func TestExpandCircular_Spec2Expansion(t *testing.T) {
	// TODO: assert repeatable results (see commented section below)

	fixturePath := filepath.Join("fixtures", "expansion", "circular-minimal.json")
	jazon, root := expandThisOrDieTrying(t, fixturePath)
	require.NotEmpty(t, jazon)

	// circular $ref are not always the same, but they sure are one of the nodes
	assertRefInJSONRegexp(t, jazon, `#/definitions/node\d+`)

	// circular $ref always resolve against the root
	assertRefResolve(t, jazon, "", root)

	// assert stripped $ref in result
	assert.NotContainsf(t, jazon, "circular-minimal.json#/",
		"expected %s to be expanded with stripped circular $ref", fixturePath)

	fixturePath = filepath.Join("fixtures", "expansion", "circularSpec2.json")
	jazon, root = expandThisOrDieTrying(t, fixturePath)
	require.NotEmpty(t, jazon)

	// circular $ref resolved against the expanded root document
	assertRefInJSON(t, jazon, `#/definitions/`)

	// circular $ref always resolve against the root
	assertRefResolve(t, jazon, "", root)

	// circular $ref can always be further expanded against the root
	assertRefExpand(t, jazon, "", root)

	assert.NotContainsf(t, jazon, "circularSpec.json#/",
		"expected %s to be expanded with stripped circular $ref", fixturePath)

	/*

		At the moment, the result of expanding circular references is not stable,
		when several cycles have intersections:
		the spec structure is randomly walked through and mutating as expansion is carried out.
		detected cycles in $ref are not necessarily the shortest matches.

		This may result in different, functionally correct expanded specs (e.g. with same validations)

			for i := 0; i < 1; i++ {
				bbb := expandThisOrDieTrying(t, fixturePath)
				t.Log(bbb)
				if !assert.JSONEqf(t, jazon, bbb, "on iteration %d, we should have stable expanded spec", i) {
					t.FailNow()
					return
				}
			}
	*/
}

func TestExpandCircular_MoreCircular(t *testing.T) {
	// Additional testcase for circular $ref (from go-openapi/validate):
	// - $ref with file = current file
	// - circular is located in remote file
	//
	// There are 4 variants to run:
	// - with/without $ref with local file (so its not really remote)
	// - with circular in a schema in  #/responses
	// - with circular in a schema in  #/parameters

	fixturePath := filepath.Join("fixtures", "more_circulars", "spec.json")
	jazon, root := expandThisOrDieTrying(t, fixturePath)
	require.NotEmpty(t, jazon)
	assertRefInJSON(t, jazon, "item.json#/item")
	assertRefResolve(t, jazon, "", root, &ExpandOptions{RelativeBase: fixturePath})

	fixturePath = filepath.Join("fixtures", "more_circulars", "spec2.json")
	jazon, root = expandThisOrDieTrying(t, fixturePath)
	require.NotEmpty(t, jazon)
	assertRefInJSON(t, jazon, "item2.json#/item")
	assertRefResolve(t, jazon, "", root, &ExpandOptions{RelativeBase: fixturePath})

	fixturePath = filepath.Join("fixtures", "more_circulars", "spec3.json")
	jazon, root = expandThisOrDieTrying(t, fixturePath)
	require.NotEmpty(t, jazon)
	assertRefInJSON(t, jazon, "item.json#/item")
	assertRefResolve(t, jazon, "", root, &ExpandOptions{RelativeBase: fixturePath})

	fixturePath = filepath.Join("fixtures", "more_circulars", "spec4.json")
	jazon, root = expandThisOrDieTrying(t, fixturePath)
	require.NotEmpty(t, jazon)
	assertRefInJSON(t, jazon, "item4.json#/item")
	assertRefResolve(t, jazon, "", root, &ExpandOptions{RelativeBase: fixturePath})
}

func TestExpandCircular_Issue957(t *testing.T) {
	fixturePath := filepath.Join("fixtures", "bugs", "957", "fixture-957.json")
	jazon, root := expandThisOrDieTrying(t, fixturePath)
	require.NotEmpty(t, jazon)

	require.NotContainsf(t, jazon, "fixture-957.json#/",
		"expected %s to be expanded with stripped circular $ref", fixturePath)

	assertRefInJSON(t, jazon, "#/definitions/")

	assertRefResolve(t, jazon, "", root)

	assertRefExpand(t, jazon, "", root)
}

func TestExpandCircular_Bitbucket(t *testing.T) {
	// Additional testcase for circular $ref (from bitbucket api)

	fixturePath := filepath.Join("fixtures", "more_circulars", "bitbucket.json")
	jazon, root := expandThisOrDieTrying(t, fixturePath)
	require.NotEmpty(t, jazon)

	assertRefInJSON(t, jazon, "#/definitions/")

	assertRefResolve(t, jazon, "", root)

	assertRefExpand(t, jazon, "", root)
}

func TestExpandCircular_ResponseWithRoot(t *testing.T) {
	rootDoc := new(Swagger)
	b, err := ioutil.ReadFile(filepath.Join("fixtures", "more_circulars", "resp.json"))
	require.NoError(t, err)

	require.NoError(t, json.Unmarshal(b, rootDoc))

	path := rootDoc.Paths.Paths["/api/v1/getx"]
	resp := path.Post.Responses.StatusCodeResponses[200]

	thisCache := cacheOrDefault(nil)

	// during the first response expand, refs are getting expanded,
	// so the following expands cannot properly resolve them w/o the document.
	// this happens in validator.Validate() when different validators try to expand the same mutable response.
	require.NoError(t, ExpandResponseWithRoot(&resp, rootDoc, thisCache))

	jazon := asJSON(t, resp)
	assertRefInJSON(t, jazon, "#/definitions/MyObj")

	// do it again
	require.NoError(t, ExpandResponseWithRoot(&resp, rootDoc, thisCache))
	jazon = asJSON(t, resp)
	assertRefInJSON(t, jazon, "#/definitions/MyObj")
}

func TestExpandCircular_Issue415(t *testing.T) {
	jazon, root := expandThisOrDieTrying(t, filepath.Join("fixtures", "expansion", "clickmeter.json"))
	require.NotEmpty(t, jazon)

	assertRefInJSON(t, jazon, "#/definitions/")
	assertRefResolve(t, jazon, "", root)
	assertRefExpand(t, jazon, "", root)
}

func TestExpandCircular_SpecExpansion(t *testing.T) {
	jazon, root := expandThisOrDieTrying(t, filepath.Join("fixtures", "expansion", "circularSpec.json"))
	require.NotEmpty(t, jazon)

	assertRefInJSON(t, jazon, "#/definitions/Book")
	assertRefResolve(t, jazon, "", root)
	assertRefExpand(t, jazon, "", root)
}

func TestExpandCircular_RemoteCircularID(t *testing.T) {
	go func() {
		err := http.ListenAndServe("localhost:1234", http.FileServer(http.Dir("fixtures/more_circulars/remote")))
		if err != nil {
			panic(err.Error())
		}
	}()
	time.Sleep(100 * time.Millisecond)

	// from json-schema test suite testcase for remote with circular ID
	fixturePath := "http://localhost:1234/tree"
	jazon, root := expandThisSchemaOrDieTrying(t, fixturePath)
	assertRefResolve(t, jazon, "", root, &ExpandOptions{RelativeBase: fixturePath})
	assertRefExpand(t, jazon, "", root, &ExpandOptions{RelativeBase: fixturePath})

	assert.NoError(t, ExpandSchemaWithBasePath(root, nil, &ExpandOptions{}))

	jazon = asJSON(t, root)

	assertRefInJSONRegexp(t, jazon, "^http://localhost:1234/tree$") // $ref now point to the root doc

	// a spec using the previous circular schema
	fixtureSpecPath := filepath.Join("fixtures", "more_circulars", "with-id.json")
	jazon, doc := expandThisOrDieTrying(t, fixtureSpecPath)

	assertRefInJSON(t, jazon, fixturePath) // all remaining $ref's point to the circular ID (http://...)

	// ResolveRef fails, because there are some remote $ref, but ResolveRefWithBasePath is successful
	assertRefResolve(t, jazon, "", doc, &ExpandOptions{})
	assertRefExpand(t, jazon, "", doc)
}

func TestCircular_RemoteExpandAzure(t *testing.T) {
	// local copy of : https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/specification/network/resource-manager/Microsoft.Network/stable/2020-04-01/publicIpAddress.json
	server := httptest.NewServer(http.FileServer(http.Dir("fixtures/azure")))
	defer server.Close()

	basePath := server.URL + "/publicIpAddress.json"
	jazon, sch := expandThisOrDieTrying(t, basePath)

	// check a pointer with escaped path
	pth1, err := ResolvePathItem(sch, MustCreateRef("#/paths/~1subscriptions~1%7BsubscriptionId%7D~1providers~1Microsoft.Network~1publicIPAddresses"), nil)
	require.NoError(t, err)
	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/)`)

	// check all $ref resolve in the expanded root
	// (filter out the remaining $ref in x-ms-example extensions, which are not expanded)
	t.Run("resolve $ref azure", func(t *testing.T) {
		assertRefResolve(t, jazon, `\./example`, sch, &ExpandOptions{RelativeBase: basePath})
	})
}

Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
25 Jan 2024 4.43 PM
root / root
0755
.github
--
25 Jan 2024 4.43 PM
root / root
0555
fixtures
--
25 Jan 2024 4.43 PM
root / root
0555
schemas
--
25 Jan 2024 4.43 PM
root / root
0555
.editorconfig
0.526 KB
25 Jan 2024 4.43 PM
root / root
0444
.gitignore
0.024 KB
25 Jan 2024 4.43 PM
root / root
0444
.golangci.yml
0.621 KB
25 Jan 2024 4.43 PM
root / root
0444
CODE_OF_CONDUCT.md
3.157 KB
25 Jan 2024 4.43 PM
root / root
0444
LICENSE
11.092 KB
25 Jan 2024 4.43 PM
root / root
0444
README.md
2.114 KB
25 Jan 2024 4.43 PM
root / root
0444
appveyor.yml
1.261 KB
25 Jan 2024 4.43 PM
root / root
0444
auth_test.go
4.577 KB
25 Jan 2024 4.43 PM
root / root
0444
bindata.go
24.402 KB
25 Jan 2024 4.43 PM
root / root
0444
cache.go
2.507 KB
25 Jan 2024 4.43 PM
root / root
0444
cache_test.go
0.66 KB
25 Jan 2024 4.43 PM
root / root
0444
circular_test.go
9.746 KB
25 Jan 2024 4.43 PM
root / root
0444
contact_info.go
1.574 KB
25 Jan 2024 4.43 PM
root / root
0444
contact_info_test.go
1.422 KB
25 Jan 2024 4.43 PM
root / root
0444
debug.go
1.272 KB
25 Jan 2024 4.43 PM
root / root
0444
debug_test.go
1.293 KB
25 Jan 2024 4.43 PM
root / root
0444
errors.go
0.881 KB
25 Jan 2024 4.43 PM
root / root
0444
expander.go
16.719 KB
25 Jan 2024 4.43 PM
root / root
0444
expander_test.go
40.121 KB
25 Jan 2024 4.43 PM
root / root
0444
external_docs.go
0.896 KB
25 Jan 2024 4.43 PM
root / root
0444
external_docs_test.go
1.045 KB
25 Jan 2024 4.43 PM
root / root
0444
go.mod
0.236 KB
25 Jan 2024 4.43 PM
root / root
0444
go.sum
3.695 KB
25 Jan 2024 4.43 PM
root / root
0444
header.go
4.943 KB
25 Jan 2024 4.43 PM
root / root
0444
header_test.go
4.348 KB
25 Jan 2024 4.43 PM
root / root
0444
helpers_spec_test.go
3.971 KB
25 Jan 2024 4.43 PM
root / root
0444
helpers_test.go
4.854 KB
25 Jan 2024 4.43 PM
root / root
0444
info.go
4.256 KB
25 Jan 2024 4.43 PM
root / root
0444
info_test.go
2.407 KB
25 Jan 2024 4.43 PM
root / root
0444
items.go
5.755 KB
25 Jan 2024 4.43 PM
root / root
0444
items_test.go
4.494 KB
25 Jan 2024 4.43 PM
root / root
0444
license.go
1.48 KB
25 Jan 2024 4.43 PM
root / root
0444
license_test.go
1.322 KB
25 Jan 2024 4.43 PM
root / root
0444
normalizer.go
6.241 KB
25 Jan 2024 4.43 PM
root / root
0444
normalizer_nonwindows.go
1.191 KB
25 Jan 2024 4.43 PM
root / root
0444
normalizer_test.go
16.808 KB
25 Jan 2024 4.43 PM
root / root
0444
normalizer_windows.go
5.146 KB
25 Jan 2024 4.43 PM
root / root
0444
operation.go
10.751 KB
25 Jan 2024 4.43 PM
root / root
0444
operation_test.go
9.549 KB
25 Jan 2024 4.43 PM
root / root
0444
parameter.go
10.344 KB
25 Jan 2024 4.43 PM
root / root
0444
parameters_test.go
4.756 KB
25 Jan 2024 4.43 PM
root / root
0444
path_item.go
2.596 KB
25 Jan 2024 4.43 PM
root / root
0444
path_item_test.go
2.42 KB
25 Jan 2024 4.43 PM
root / root
0444
paths.go
2.571 KB
25 Jan 2024 4.43 PM
root / root
0444
paths_test.go
1.167 KB
25 Jan 2024 4.43 PM
root / root
0444
properties.go
2.403 KB
25 Jan 2024 4.43 PM
root / root
0444
properties_test.go
2.054 KB
25 Jan 2024 4.43 PM
root / root
0444
ref.go
4.167 KB
25 Jan 2024 4.43 PM
root / root
0444
ref_test.go
1.19 KB
25 Jan 2024 4.43 PM
root / root
0444
resolver.go
3.752 KB
25 Jan 2024 4.43 PM
root / root
0444
resolver_test.go
12.791 KB
25 Jan 2024 4.43 PM
root / root
0444
response.go
4.166 KB
25 Jan 2024 4.43 PM
root / root
0444
response_test.go
2.817 KB
25 Jan 2024 4.43 PM
root / root
0444
responses.go
3.619 KB
25 Jan 2024 4.43 PM
root / root
0444
schema.go
18.14 KB
25 Jan 2024 4.43 PM
root / root
0444
schema_loader.go
8.993 KB
25 Jan 2024 4.43 PM
root / root
0444
schema_test.go
6.643 KB
25 Jan 2024 4.43 PM
root / root
0444
security_scheme.go
5.713 KB
25 Jan 2024 4.43 PM
root / root
0444
spec.go
2.18 KB
25 Jan 2024 4.43 PM
root / root
0444
spec_test.go
12.704 KB
25 Jan 2024 4.43 PM
root / root
0444
structs_test.go
3.88 KB
25 Jan 2024 4.43 PM
root / root
0444
swagger.go
11.854 KB
25 Jan 2024 4.43 PM
root / root
0444
swagger_test.go
11.273 KB
25 Jan 2024 4.43 PM
root / root
0444
tag.go
2.235 KB
25 Jan 2024 4.43 PM
root / root
0444
url_go18.go
0.093 KB
25 Jan 2024 4.43 PM
root / root
0444
url_go19.go
0.189 KB
25 Jan 2024 4.43 PM
root / root
0444
validations.go
7.121 KB
25 Jan 2024 4.43 PM
root / root
0444
validations_test.go
2.969 KB
25 Jan 2024 4.43 PM
root / root
0444
xml_object.go
1.928 KB
25 Jan 2024 4.43 PM
root / root
0444
xml_object_test.go
1.938 KB
25 Jan 2024 4.43 PM
root / root
0444

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF