Skip to content

Commit 9afb06a

Browse files
committed
Updated the project's README and godocs
* removed golangci badge * made it explicit the repo won't support swagger 3 * added some missing godoc comments * run gofmt * reworked one complex unit test for readability * closes #21 Signed-off-by: Frederic BIDON <[email protected]>
1 parent d0486ac commit 9afb06a

File tree

6 files changed

+93
-49
lines changed

6 files changed

+93
-49
lines changed

README.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,29 @@
22

33
[![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/go-openapi/spec/master/LICENSE)
44
[![GoDoc](https://godoc.org/github.com/go-openapi/spec?status.svg)](http://godoc.org/github.com/go-openapi/spec)
5-
[![GolangCI](https://golangci.com/badges/github.com/go-openapi/spec.svg)](https://golangci.com)
65
[![Go Report Card](https://goreportcard.com/badge/github.com/go-openapi/spec)](https://goreportcard.com/report/github.com/go-openapi/spec)
76

87
The object model for OpenAPI specification documents.
98

10-
Currently supports Swagger 2.0.
9+
### FAQ
10+
11+
* What does this do?
12+
13+
> 1. This package knows how to marshal and unmarshal Swagger API specifications into a golang object model
14+
> 2. It knows how to resolve $ref and expand them to make a single root documment
15+
16+
* How does it play with the rest of the go-openapi packages ?
17+
18+
> 1. This package is a the core of the go-openapi suite of packages and [code generator](https://github.com/go-swagger/go-swagger)
19+
> 2. There is [spec loading package](https://github.com/go-openapi/loads) to fetch specs as JSON or YAML from local or remote locations
20+
> 3. There is [spec validation package](https://github.com/go-openapi/validate) built on top of it
21+
> 4. There is [spec analysis package](https://github.com/go-openapi/analysis) built on top of it, to analyze, flatten, fix and merge spec documents
22+
23+
* Does this library support OpenAPI 3?
24+
25+
> No.
26+
> This package currently only supports OpenAPI 2.0 (aka Swagger 2.0).
27+
> There is no plan to make it evolve toward supporting OpenAPI 3.x.
28+
> This [discussion thread](https://github.com/go-openapi/spec/issues/21) relates the full story.
29+
>
30+
> An early attempt to support Swagger 3 may be found at: https://github.com/go-openapi/spec3

bindata.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contact_info.go

+3
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ type ContactInfo struct {
2828
VendorExtensible
2929
}
3030

31+
// ContactInfoProps hold the properties of a ContactInfo object
3132
type ContactInfoProps struct {
3233
Name string `json:"name,omitempty"`
3334
URL string `json:"url,omitempty"`
3435
Email string `json:"email,omitempty"`
3536
}
3637

38+
// UnmarshalJSON hydrates ContactInfo from json
3739
func (c *ContactInfo) UnmarshalJSON(data []byte) error {
3840
if err := json.Unmarshal(data, &c.ContactInfoProps); err != nil {
3941
return err
4042
}
4143
return json.Unmarshal(data, &c.VendorExtensible)
4244
}
4345

46+
// MarshalJSON produces ContactInfo as json
4447
func (c ContactInfo) MarshalJSON() ([]byte, error) {
4548
b1, err := json.Marshal(c.ContactInfoProps)
4649
if err != nil {

license.go

+3
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@ type License struct {
2828
VendorExtensible
2929
}
3030

31+
// LicenseProps holds the properties of a License object
3132
type LicenseProps struct {
3233
Name string `json:"name,omitempty"`
3334
URL string `json:"url,omitempty"`
3435
}
3536

37+
// UnmarshalJSON hydrates License from json
3638
func (l *License) UnmarshalJSON(data []byte) error {
3739
if err := json.Unmarshal(data, &l.LicenseProps); err != nil {
3840
return err
3941
}
4042
return json.Unmarshal(data, &l.VendorExtensible)
4143
}
4244

45+
// MarshalJSON produces License as json
4346
func (l License) MarshalJSON() ([]byte, error) {
4447
b1, err := json.Marshal(l.LicenseProps)
4548
if err != nil {

properties.go

+9
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ import (
77
"sort"
88
)
99

10+
// OrderSchemaItem holds a named schema (e.g. from a property of an object)
1011
type OrderSchemaItem struct {
1112
Name string
1213
Schema
1314
}
1415

16+
// OrderSchemaItems is a sortable slice of named schemas.
17+
// The ordering is defined by the x-order schema extension.
1518
type OrderSchemaItems []OrderSchemaItem
1619

20+
// MarshalJSON produces a json object with keys defined by the name schemas
21+
// of the OrderSchemaItems slice, keeping the original order of the slice.
1722
func (items OrderSchemaItems) MarshalJSON() ([]byte, error) {
1823
buf := bytes.NewBuffer(nil)
1924
buf.WriteString("{")
@@ -60,8 +65,11 @@ func (items OrderSchemaItems) Less(i, j int) (ret bool) {
6065
return items[i].Name < items[j].Name
6166
}
6267

68+
// SchemaProperties is a map representing the properties of a Schema object.
69+
// It knows how to transform its keys into an ordered slice.
6370
type SchemaProperties map[string]Schema
6471

72+
// ToOrderedSchemaItems transforms the map of properties into a sortable slice
6573
func (properties SchemaProperties) ToOrderedSchemaItems() OrderSchemaItems {
6674
items := make(OrderSchemaItems, 0, len(properties))
6775
for k, v := range properties {
@@ -74,6 +82,7 @@ func (properties SchemaProperties) ToOrderedSchemaItems() OrderSchemaItems {
7482
return items
7583
}
7684

85+
// MarshalJSON produces properties as json, keeping their order.
7786
func (properties SchemaProperties) MarshalJSON() ([]byte, error) {
7887
if properties == nil {
7988
return []byte("null"), nil

spec_test.go

+53-44
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,8 @@ func Test_Issue1429(t *testing.T) {
7272
// assert well expanded
7373
require.Truef(t, (sp.Paths != nil && sp.Paths.Paths != nil), "expected paths to be available in fixture")
7474

75-
for _, pi := range sp.Paths.Paths {
76-
for _, param := range pi.Get.Parameters {
77-
if assert.NotNilf(t, param.Schema, "expected param schema not to be nil") {
78-
// all param fixtures are body param with schema
79-
// all $ref expanded
80-
assert.Equal(t, "", param.Schema.Ref.String())
81-
}
82-
}
83-
for code, response := range pi.Get.Responses.StatusCodeResponses {
84-
// all response fixtures are with StatusCodeResponses, but 200
85-
if code == 200 {
86-
assert.Nilf(t, response.Schema, "expected response schema to be nil")
87-
continue
88-
}
89-
if assert.NotNilf(t, response.Schema, "expected response schema not to be nil") {
90-
assert.Equal(t, "", response.Schema.Ref.String())
91-
}
92-
}
93-
}
75+
assertPaths1429(t, sp)
76+
9477
for _, def := range sp.Definitions {
9578
assert.Equal(t, "", def.Ref.String())
9679
}
@@ -103,48 +86,74 @@ func Test_Issue1429(t *testing.T) {
10386
// assert well resolved
10487
require.Truef(t, (sp.Paths != nil && sp.Paths.Paths != nil), "expected paths to be available in fixture")
10588

89+
assertPaths1429SkipSchema(t, sp)
90+
91+
for _, def := range sp.Definitions {
92+
assert.Contains(t, def.Ref.String(), "responses.yaml#/")
93+
}
94+
}
95+
96+
func assertPaths1429(t testing.TB, sp *spec.Swagger) {
10697
for _, pi := range sp.Paths.Paths {
10798
for _, param := range pi.Get.Parameters {
108-
if assert.NotNilf(t, param.Schema, "expected param schema not to be nil") {
109-
// all param fixtures are body param with schema
110-
if param.Name == "plainRequest" {
111-
// this one is expanded
112-
assert.Equal(t, "", param.Schema.Ref.String())
113-
continue
114-
}
115-
if param.Name == "nestedBody" {
116-
// this one is local
117-
assert.Truef(t, strings.HasPrefix(param.Schema.Ref.String(), "#/definitions/"),
118-
"expected rooted definitions $ref, got: %s", param.Schema.Ref.String())
119-
continue
120-
}
121-
if param.Name == "remoteRequest" {
122-
assert.Contains(t, param.Schema.Ref.String(), "remote/remote.yaml#/")
123-
continue
124-
}
125-
assert.Contains(t, param.Schema.Ref.String(), "responses.yaml#/")
126-
}
99+
require.NotNilf(t, param.Schema, "expected param schema not to be nil")
100+
// all param fixtures are body param with schema
101+
// all $ref expanded
102+
assert.Equal(t, "", param.Schema.Ref.String())
127103
}
104+
128105
for code, response := range pi.Get.Responses.StatusCodeResponses {
129106
// all response fixtures are with StatusCodeResponses, but 200
130107
if code == 200 {
131108
assert.Nilf(t, response.Schema, "expected response schema to be nil")
132109
continue
133110
}
134-
if code == 204 {
135-
assert.Contains(t, response.Schema.Ref.String(), "remote/remote.yaml#/")
111+
require.NotNilf(t, response.Schema, "expected response schema not to be nil")
112+
assert.Equal(t, "", response.Schema.Ref.String())
113+
}
114+
}
115+
}
116+
117+
func assertPaths1429SkipSchema(t testing.TB, sp *spec.Swagger) {
118+
for _, pi := range sp.Paths.Paths {
119+
for _, param := range pi.Get.Parameters {
120+
require.NotNilf(t, param.Schema, "expected param schema not to be nil")
121+
122+
// all param fixtures are body param with schema
123+
switch param.Name {
124+
case "plainRequest":
125+
// this one is expanded
126+
assert.Equal(t, "", param.Schema.Ref.String())
127+
continue
128+
case "nestedBody":
129+
// this one is local
130+
assert.Truef(t, strings.HasPrefix(param.Schema.Ref.String(), "#/definitions/"),
131+
"expected rooted definitions $ref, got: %s", param.Schema.Ref.String())
132+
continue
133+
case "remoteRequest":
134+
assert.Contains(t, param.Schema.Ref.String(), "remote/remote.yaml#/")
136135
continue
137136
}
138-
if code == 404 {
137+
assert.Contains(t, param.Schema.Ref.String(), "responses.yaml#/")
138+
139+
}
140+
141+
for code, response := range pi.Get.Responses.StatusCodeResponses {
142+
// all response fixtures are with StatusCodeResponses, but 200
143+
switch code {
144+
case 200:
145+
assert.Nilf(t, response.Schema, "expected response schema to be nil")
146+
continue
147+
case 204:
148+
assert.Contains(t, response.Schema.Ref.String(), "remote/remote.yaml#/")
149+
continue
150+
case 404:
139151
assert.Equal(t, "", response.Schema.Ref.String())
140152
continue
141153
}
142154
assert.Containsf(t, response.Schema.Ref.String(), "responses.yaml#/", "expected remote ref at resp. %d", code)
143155
}
144156
}
145-
for _, def := range sp.Definitions {
146-
assert.Contains(t, def.Ref.String(), "responses.yaml#/")
147-
}
148157
}
149158

150159
func Test_MoreLocalExpansion(t *testing.T) {

0 commit comments

Comments
 (0)