コード例 #1
0
ファイル: expander_test.go プロジェクト: vikstrous/go-swagger
func TestResolveLocalRef(t *testing.T) {
	rootDoc := new(Swagger)
	json.Unmarshal(testingutil.PetStoreJSONMessage, rootDoc)

	Convey("resolving local a ref", t, func() {

		Convey("in a swagger spec", func() {

			Convey("to a schema", func() {

				Convey("resolves root to same ptr instance", func() {
					result := new(Swagger)
					ref, _ := NewRef("#")
					resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
					err := resolver.Resolve(&ref, result)
					So(err, ShouldBeNil)
					So(result, ShouldResemble, rootDoc)
				})

				Convey("from a fragment", func() {
					var tgt Schema
					ref, err := NewRef("#/definitions/Category")
					So(err, ShouldBeNil)

					resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
					err = resolver.Resolve(&ref, &tgt)
					So(err, ShouldBeNil)
					So(tgt.ID, ShouldEqual, "Category")
				})

				Convey("from an invalid fragment", func() {
					var tgt Schema
					ref, err := NewRef("#/definitions/NotThere")
					So(err, ShouldBeNil)

					resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
					err = resolver.Resolve(&ref, &tgt)
					So(err, ShouldNotBeNil)
				})

			})

			Convey("to a parameter", func() {
				rootDoc = new(Swagger)
				b, err := ioutil.ReadFile("../fixtures/specs/refed.json")
				So(err, ShouldBeNil)
				json.Unmarshal(b, rootDoc)

				var tgt Parameter
				ref, err := NewRef("#/parameters/idParam")
				So(err, ShouldBeNil)

				resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
				err = resolver.Resolve(&ref, &tgt)
				So(err, ShouldBeNil)
				So(tgt.Name, ShouldEqual, "id")
				So(tgt.In, ShouldEqual, "path")
				So(tgt.Description, ShouldEqual, "ID of pet to fetch")
				So(tgt.Required, ShouldBeTrue)
				So(tgt.Type, ShouldEqual, "integer")
				So(tgt.Format, ShouldEqual, "int64")
			})

			Convey("to a path item object", func() {
				rootDoc = new(Swagger)
				b, err := ioutil.ReadFile("../fixtures/specs/refed.json")
				So(err, ShouldBeNil)
				json.Unmarshal(b, rootDoc)

				var tgt PathItem
				ref, err := NewRef("#/paths/" + jsonpointer.Escape("/pets/{id}"))
				So(err, ShouldBeNil)

				resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
				err = resolver.Resolve(&ref, &tgt)
				So(err, ShouldBeNil)
				So(tgt.Get, ShouldEqual, rootDoc.Paths.Paths["/pets/{id}"].Get)
			})

			Convey("to a response object", func() {
				rootDoc = new(Swagger)
				b, err := ioutil.ReadFile("../fixtures/specs/refed.json")
				So(err, ShouldBeNil)
				json.Unmarshal(b, rootDoc)

				var tgt Response
				ref, err := NewRef("#/responses/petResponse")
				So(err, ShouldBeNil)

				resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
				err = resolver.Resolve(&ref, &tgt)
				So(err, ShouldBeNil)
				So(tgt, ShouldResemble, rootDoc.Responses["petResponse"])
			})

		})
	})

}
コード例 #2
0
ファイル: expander_test.go プロジェクト: vikstrous/go-swagger
func TestResolveRemoteRef(t *testing.T) {
	specs := "../fixtures/specs"
	fileserver := http.FileServer(http.Dir(specs))

	Convey("resolving a remote ref", t, func() {
		server := httptest.NewServer(fileserver)
		Reset(func() {
			server.Close()
		})

		Convey("in a swagger spec", func() {
			rootDoc := new(Swagger)
			b, err := ioutil.ReadFile("../fixtures/specs/refed.json")
			So(err, ShouldBeNil)
			json.Unmarshal(b, rootDoc)

			Convey("resolves root to same schema", func() {
				var result Swagger
				ref, _ := NewRef(server.URL + "/refed.json#")
				resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
				err = resolver.Resolve(&ref, &result)
				So(err, ShouldBeNil)
				compareSpecs(result, *rootDoc)
			})

			Convey("to a schema", func() {

				Convey("from a fragment", func() {
					var tgt Schema
					ref, err := NewRef(server.URL + "/refed.json#/definitions/pet")
					So(err, ShouldBeNil)
					resolver := &schemaLoader{root: rootDoc, cache: defaultResolutionCache(), loadDoc: swag.JSONDoc}
					err = resolver.Resolve(&ref, &tgt)
					So(err, ShouldBeNil)
					So(tgt.Required, ShouldResemble, []string{"id", "name"})
				})

				Convey("from an invalid fragment", func() {
					var tgt Schema
					ref, err := NewRef(server.URL + "/refed.json#/definitions/NotThere")
					So(err, ShouldBeNil)

					resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
					err = resolver.Resolve(&ref, &tgt)
					So(err, ShouldNotBeNil)
				})

				Convey("with a resolution context", func() {
					server.Close()
					server = resolutionContextServer()
					var tgt Schema
					ref, err := NewRef(server.URL + "/resolution.json#/definitions/bool")
					So(err, ShouldBeNil)

					resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
					err = resolver.Resolve(&ref, &tgt)
					So(err, ShouldBeNil)
					So(tgt.Type, ShouldResemble, StringOrArray([]string{"boolean"}))
				})

				Convey("with a nested resolution context", func() {
					server.Close()
					server = resolutionContextServer()
					var tgt Schema
					ref, err := NewRef(server.URL + "/resolution.json#/items/items")
					So(err, ShouldBeNil)

					resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
					err = resolver.Resolve(&ref, &tgt)
					So(err, ShouldBeNil)
					So(tgt.Type, ShouldResemble, StringOrArray([]string{"string"}))
				})

				Convey("with a nested resolution context with a fragment", func() {
					server.Close()
					server = resolutionContextServer()
					var tgt Schema
					ref, err := NewRef(server.URL + "/resolution2.json#/items/items")
					So(err, ShouldBeNil)

					resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
					err = resolver.Resolve(&ref, &tgt)
					So(err, ShouldBeNil)
					So(tgt.Type, ShouldResemble, StringOrArray([]string{"file"}))
				})
			})

			Convey("to a parameter", func() {
				var tgt Parameter
				ref, err := NewRef(server.URL + "/refed.json#/parameters/idParam")
				So(err, ShouldBeNil)

				resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
				err = resolver.Resolve(&ref, &tgt)
				So(err, ShouldBeNil)
				So(tgt.Name, ShouldEqual, "id")
				So(tgt.In, ShouldEqual, "path")
				So(tgt.Description, ShouldEqual, "ID of pet to fetch")
				So(tgt.Required, ShouldBeTrue)
				So(tgt.Type, ShouldEqual, "integer")
				So(tgt.Format, ShouldEqual, "int64")
			})

			Convey("to a path item object", func() {
				var tgt PathItem
				ref, err := NewRef(server.URL + "/refed.json#/paths/" + jsonpointer.Escape("/pets/{id}"))
				So(err, ShouldBeNil)

				resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
				err = resolver.Resolve(&ref, &tgt)
				So(err, ShouldBeNil)
				So(tgt.Get, ShouldResemble, rootDoc.Paths.Paths["/pets/{id}"].Get)
			})

			Convey("to a response object", func() {
				var tgt Response
				ref, err := NewRef(server.URL + "/refed.json#/responses/petResponse")
				So(err, ShouldBeNil)

				resolver, _ := defaultSchemaLoader(rootDoc, nil, nil)
				err = resolver.Resolve(&ref, &tgt)
				So(err, ShouldBeNil)
				So(tgt, ShouldResemble, rootDoc.Responses["petResponse"])
			})
		})
	})

}