@@ -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 )
0 commit comments