Skip to content

Commit 8104239

Browse files
committed
Support response headers, dependency bump
1 parent 71d0e9f commit 8104239

File tree

4 files changed

+154
-25
lines changed

4 files changed

+154
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8-
- Add unreleased items here.
8+
- Return header examples when possible.
9+
- Update dependency versions.
910

1011
## [1.3.0] - 2019-03-18
1112
- Add `--add-server` to add a custom server when using `--validate-server`.

apisprout.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ func getTypedExample(mt *openapi3.MediaType) (interface{}, error) {
163163
}
164164

165165
// getExample tries to return an example for a given operation.
166-
func getExample(negotiator *ContentNegotiator, prefer string, op *openapi3.Operation) (int, string, interface{}, error) {
166+
func getExample(negotiator *ContentNegotiator, prefer string, op *openapi3.Operation) (int, string, map[string]*openapi3.HeaderRef, interface{}, error) {
167167
var responses []string
168+
var blankHeaders = make(map[string]*openapi3.HeaderRef)
169+
168170
if prefer == "" {
169171
// First, make a list of responses ordered by successful (200-299 status code)
170172
// before other types.
@@ -180,7 +182,7 @@ func getExample(negotiator *ContentNegotiator, prefer string, op *openapi3.Opera
180182
responses = append(success, other...)
181183
} else {
182184
if op.Responses[prefer] == nil {
183-
return 0, "", nil, ErrNoExample
185+
return 0, "", blankHeaders, nil, ErrNoExample
184186
}
185187
responses = []string{prefer}
186188
}
@@ -196,7 +198,7 @@ func getExample(negotiator *ContentNegotiator, prefer string, op *openapi3.Opera
196198

197199
if response.Value.Content == nil {
198200
// This is a valid response but has no body defined.
199-
return status, "", "", nil
201+
return status, "", blankHeaders, "", nil
200202
}
201203

202204
for mt, content := range response.Value.Content {
@@ -207,14 +209,14 @@ func getExample(negotiator *ContentNegotiator, prefer string, op *openapi3.Opera
207209

208210
example, err := getTypedExample(content)
209211
if err == nil {
210-
return status, mt, example, nil
212+
return status, mt, response.Value.Headers, example, nil
211213
}
212214

213215
fmt.Printf("Error getting example: %v\n", err)
214216
}
215217
}
216218

217-
return 0, "", nil, ErrNoExample
219+
return 0, "", blankHeaders, nil, ErrNoExample
218220
}
219221

220222
// addLocalServers will ensure that requests to localhost are always allowed
@@ -540,7 +542,7 @@ func server(cmd *cobra.Command, args []string) {
540542
prefer = ""
541543
}
542544

543-
status, mediatype, example, err := getExample(negotiator, prefer, route.Operation)
545+
status, mediatype, headers, example, err := getExample(negotiator, prefer, route.Operation)
544546
if err != nil {
545547
log.Printf("%s => Missing example", info)
546548
w.WriteHeader(http.StatusTeapot)
@@ -579,8 +581,26 @@ func server(cmd *cobra.Command, args []string) {
579581
}
580582
}
581583

584+
for name, header := range headers {
585+
if header.Value != nil {
586+
example := name
587+
588+
if header.Value.Schema != nil && header.Value.Schema.Value != nil {
589+
if v, err := OpenAPIExample(ModeResponse, header.Value.Schema.Value); err == nil {
590+
if vs, ok := v.(string); ok {
591+
example = vs
592+
} else {
593+
fmt.Printf("Could not convert example value '%v' to string", v)
594+
}
595+
}
596+
}
597+
598+
w.Header().Set(name, example)
599+
}
600+
}
601+
582602
if mediatype != "" {
583-
w.Header().Add("Content-Type", mediatype)
603+
w.Header().Set("Content-Type", mediatype)
584604
}
585605

586606
w.WriteHeader(status)

go.mod

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ module github.com/danielgtaylor/apisprout
33
go 1.12
44

55
require (
6-
github.com/BurntSushi/toml v0.3.1 // indirect
6+
github.com/davecgh/go-spew v1.1.1
77
github.com/fsnotify/fsnotify v1.4.7
8-
github.com/getkin/kin-openapi v0.1.1-0.20190210195911-82a13f94c08e
8+
github.com/getkin/kin-openapi v0.2.0
99
github.com/gobwas/glob v0.2.3
10-
github.com/inconshreveable/mousetrap v1.0.0 // indirect
10+
github.com/magiconair/properties v1.8.1 // indirect
11+
github.com/pelletier/go-toml v1.4.0 // indirect
1112
github.com/pkg/errors v0.8.1
12-
github.com/spf13/afero v1.2.1 // indirect
13-
github.com/spf13/cobra v0.0.3
13+
github.com/spf13/afero v1.2.2 // indirect
14+
github.com/spf13/cobra v0.0.4
15+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
1416
github.com/spf13/pflag v1.0.3
15-
github.com/spf13/viper v1.3.1
17+
github.com/spf13/viper v1.4.0
1618
github.com/stretchr/testify v1.3.0
17-
golang.org/x/sys v0.0.0-20190226215855-775f8194d0f9 // indirect
19+
golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1 // indirect
20+
golang.org/x/text v0.3.2 // indirect
1821
gopkg.in/yaml.v2 v2.2.2
1922
)

0 commit comments

Comments
 (0)