Skip to content

Commit 04181a7

Browse files
authored
Added named json string function (#5)
* fixed all examples to have a generated name, added WithNamedJsonString to allow adding json string examples with a specific name * fixed tests * moved naming logic for examples into the add_example func, fixed a couple other small issues * added response * added extra example showing additional examples, fixed tests
1 parent f43202b commit 04181a7

4 files changed

Lines changed: 36 additions & 30 deletions

File tree

build_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package openapi
33
import (
44
_ "embed"
55
"errors"
6+
"testing"
7+
"time"
8+
69
"github.com/google/go-cmp/cmp"
710
"github.com/google/go-cmp/cmp/cmpopts"
811
"github.com/hydronica/trial"
9-
"testing"
10-
"time"
1112
)
1213

1314
func TestBuildSchema(t *testing.T) {
@@ -24,12 +25,6 @@ func TestBuildSchema(t *testing.T) {
2425
F3 int
2526
}
2627

27-
// test a time type
28-
type TestT struct {
29-
F1 time.Time `json:"time.time" format:"2006-01-02"`
30-
// F2 Time `json:"openapi.time"` // custom time format can be used
31-
}
32-
3328
type TestF struct {
3429
F1 int `json:"f1_int"`
3530
F2 bool `json:"f2_bool"`

openapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Tag struct {
5454
}
5555

5656
type ExternalDocs struct {
57-
Desc string `json:"description,omitempty""` // A short description of the target documentation. CommonMark syntax MAY be used for rich text representation.
57+
Desc string `json:"description,omitempty"` // A short description of the target documentation. CommonMark syntax MAY be used for rich text representation.
5858
URL string `json:"url,omitempty" required:"true"` // REQUIRED. The URL for the target documentation. Value MUST be in the format of a URL.
5959
}
6060

paths.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ type Response struct {
162162
// s is unmarshalled into a map to extract the key and value pairs
163163
// JSONStringResp || resp.JSONString(s)
164164
func (r Response) WithJSONString(s string) Response {
165+
return r.WithNamedJsonString("", s)
166+
}
167+
168+
// WithExample takes a struct and adds a json Content to the Response
169+
// name is auto generated based on the example count
170+
func (r Response) WithExample(i any) Response {
171+
return r.WithNamedExample("", i)
172+
}
173+
174+
// WithNamedJsonString takes a json string object and adds a json Content to the Response
175+
// s is unmarshalled into a map to extract the key and value pairs
176+
// JSONStringResp || resp.JSONString(s)
177+
func (r Response) WithNamedJsonString(name string, s string) Response {
165178
var m any
166179
if s[0] == '[' && s[len(s)-1] == ']' {
167180
m = make([]any, 0)
@@ -177,12 +190,7 @@ func (r Response) WithJSONString(s string) Response {
177190
Content: Content{"invalid/json": {Examples: map[string]Example{"invalid": {Value: s}}}},
178191
}
179192
}
180-
return r.WithExample(m)
181-
}
182-
183-
// WithExample takes a struct and adds a json Content to the Response
184-
func (r Response) WithExample(i any) Response {
185-
return r.WithNamedExample("", i)
193+
return r.WithNamedExample(name, m)
186194
}
187195

188196
func (r Response) WithNamedExample(name string, i any) Response {
@@ -199,28 +207,28 @@ func (r Response) WithNamedExample(name string, i any) Response {
199207
// creating a schema based on the object i passed in.
200208
// The Example name will be the title of the Schema if not provided
201209
// and any description from added to the example as well.
202-
func (m *Media) AddExample(exName string, i any) {
210+
func (m *Media) AddExample(name string, i any) {
203211
if m.Examples == nil {
204212
m.Examples = make(map[string]Example)
205213
}
206214
schema := buildSchema(i)
207215
if m.Schema.Title == "" {
208216
m.Schema = schema
209217
}
210-
if exName == "" {
211-
exName = schema.Title
218+
if name == "" {
219+
name = "Example"
212220
}
213221
ex := Example{
214222
Desc: schema.Desc,
215223
Value: i,
216224
}
217225

218226
// create unique name if key already exists
219-
if _, found := m.Examples[exName]; found {
220-
exName = exName + strconv.Itoa(len(m.Examples))
227+
if _, found := m.Examples[name]; found {
228+
name = name + " " + strconv.Itoa(len(m.Examples))
221229
}
222230

223-
m.Examples[exName] = ex
231+
m.Examples[name] = ex
224232
}
225233

226234
// RequestBody describes a single request body.
@@ -230,6 +238,10 @@ type RequestBody struct {
230238
Required bool `json:"required,omitempty"` // Determines if the request body is required in the request. Defaults to false.
231239
}
232240

241+
func (r RequestBody) WithNamedJsonString(name string, s string) RequestBody {
242+
return r.WithNamedExample(name, s)
243+
}
244+
233245
func (r RequestBody) WithJSONString(s string) RequestBody {
234246
var m any
235247
if s[0] == '[' && s[len(s)-1] == ']' {

paths_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,10 @@ func TestAddResponse(t *testing.T) {
251251
route.AddResponse(Response{
252252
Status: 200,
253253
Desc: "resp desc",
254-
}.WithJSONString(`{"status":"ok"}`))
255-
route.AddResponse(Response{Status: 400}.WithExample(struct{ Error string }{Error: "invalid request"}))
254+
}.WithJSONString(`{"status":"ok"}`).WithJSONString(`{"status":"ok"}`))
255+
256+
route.AddResponse(Response{Status: 400}.
257+
WithExample(struct{ Error string }{Error: "invalid request"}))
256258

257259
eq, diff := trial.Equal(route, &Route{
258260
path: "/test",
@@ -270,9 +272,8 @@ func TestAddResponse(t *testing.T) {
270272
Properties: map[string]Schema{"status": {Type: "string"}},
271273
},
272274
Examples: map[string]Example{
273-
"2c69c864087c4000": {
274-
Value: map[string]any{"status": "ok"},
275-
},
275+
"Example": {Value: map[string]any{"status": "ok"}},
276+
"Example 1": {Value: map[string]any{"status": "ok"}},
276277
},
277278
}},
278279
},
@@ -285,16 +286,14 @@ func TestAddResponse(t *testing.T) {
285286
Properties: map[string]Schema{"Error": {Type: "string"}},
286287
},
287288
Examples: map[string]Example{
288-
"struct { Error string }": {
289-
Value: struct{ Error string }{Error: "invalid request"},
290-
},
289+
"Example": {Value: struct{ Error string }{Error: "invalid request"}},
291290
},
292291
}},
293292
},
294293
},
295294
})
296295
if !eq {
297-
t.Logf(diff)
296+
t.Log(diff)
298297
t.Fail()
299298
}
300299

0 commit comments

Comments
 (0)