diff --git a/build_test.go b/build_test.go index 70d54fe..489a9b0 100644 --- a/build_test.go +++ b/build_test.go @@ -3,11 +3,12 @@ package openapi import ( _ "embed" "errors" + "testing" + "time" + "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/hydronica/trial" - "testing" - "time" ) func TestBuildSchema(t *testing.T) { @@ -24,12 +25,6 @@ func TestBuildSchema(t *testing.T) { F3 int } - // test a time type - type TestT struct { - F1 time.Time `json:"time.time" format:"2006-01-02"` - // F2 Time `json:"openapi.time"` // custom time format can be used - } - type TestF struct { F1 int `json:"f1_int"` F2 bool `json:"f2_bool"` diff --git a/openapi.go b/openapi.go index 8d1bb30..c01b910 100644 --- a/openapi.go +++ b/openapi.go @@ -54,7 +54,7 @@ type Tag struct { } type ExternalDocs struct { - Desc string `json:"description,omitempty""` // A short description of the target documentation. CommonMark syntax MAY be used for rich text representation. + Desc string `json:"description,omitempty"` // A short description of the target documentation. CommonMark syntax MAY be used for rich text representation. URL string `json:"url,omitempty" required:"true"` // REQUIRED. The URL for the target documentation. Value MUST be in the format of a URL. } diff --git a/paths.go b/paths.go index 3682d81..eacd261 100644 --- a/paths.go +++ b/paths.go @@ -160,6 +160,19 @@ type Response struct { // s is unmarshalled into a map to extract the key and value pairs // JSONStringResp || resp.JSONString(s) func (r Response) WithJSONString(s string) Response { + return r.WithNamedJsonString("", s) +} + +// WithExample takes a struct and adds a json Content to the Response +// name is auto generated based on the example count +func (r Response) WithExample(i any) Response { + return r.WithNamedExample("", i) +} + +// WithNamedJsonString takes a json string object and adds a json Content to the Response +// s is unmarshalled into a map to extract the key and value pairs +// JSONStringResp || resp.JSONString(s) +func (r Response) WithNamedJsonString(name string, s string) Response { var m any if s[0] == '[' && s[len(s)-1] == ']' { m = make([]any, 0) @@ -175,12 +188,7 @@ func (r Response) WithJSONString(s string) Response { Content: Content{"invalid/json": {Examples: map[string]Example{"invalid": {Value: s}}}}, } } - return r.WithExample(m) -} - -// WithExample takes a struct and adds a json Content to the Response -func (r Response) WithExample(i any) Response { - return r.WithNamedExample("", i) + return r.WithNamedExample(name, m) } func (r Response) WithNamedExample(name string, i any) Response { @@ -197,7 +205,7 @@ func (r Response) WithNamedExample(name string, i any) Response { // creating a schema based on the object i passed in. // The Example name will be the title of the Schema if not provided // and any description from added to the example as well. -func (m *Media) AddExample(exName string, i any) { +func (m *Media) AddExample(name string, i any) { if m.Examples == nil { m.Examples = make(map[string]Example) } @@ -205,8 +213,8 @@ func (m *Media) AddExample(exName string, i any) { if m.Schema.Title == "" { m.Schema = schema } - if exName == "" { - exName = schema.Title + if name == "" { + name = "Example" } ex := Example{ Desc: schema.Desc, @@ -214,11 +222,11 @@ func (m *Media) AddExample(exName string, i any) { } // create unique name if key already exists - if _, found := m.Examples[exName]; found { - exName = exName + strconv.Itoa(len(m.Examples)) + if _, found := m.Examples[name]; found { + name = name + " " + strconv.Itoa(len(m.Examples)) } - m.Examples[exName] = ex + m.Examples[name] = ex } // RequestBody describes a single request body. @@ -228,6 +236,10 @@ type RequestBody struct { Required bool `json:"required,omitempty"` // Determines if the request body is required in the request. Defaults to false. } +func (r RequestBody) WithNamedJsonString(name string, s string) RequestBody { + return r.WithNamedExample(name, s) +} + func (r RequestBody) WithJSONString(s string) RequestBody { var m any if s[0] == '[' && s[len(s)-1] == ']' { diff --git a/paths_test.go b/paths_test.go index 1366df5..1a4011a 100644 --- a/paths_test.go +++ b/paths_test.go @@ -1,8 +1,9 @@ package openapi import ( - "github.com/hydronica/trial" "testing" + + "github.com/hydronica/trial" ) func TestAddParams(t *testing.T) { @@ -250,8 +251,10 @@ func TestAddResponse(t *testing.T) { route.AddResponse(Response{ Status: 200, Desc: "resp desc", - }.WithJSONString(`{"status":"ok"}`)) - route.AddResponse(Response{Status: 400}.WithExample(struct{ Error string }{Error: "invalid request"})) + }.WithJSONString(`{"status":"ok"}`).WithJSONString(`{"status":"ok"}`)) + + route.AddResponse(Response{Status: 400}. + WithExample(struct{ Error string }{Error: "invalid request"})) eq, diff := trial.Equal(route, &Route{ path: "/test", @@ -269,9 +272,8 @@ func TestAddResponse(t *testing.T) { Properties: map[string]Schema{"status": {Type: "string"}}, }, Examples: map[string]Example{ - "2c69c864087c4000": { - Value: map[string]any{"status": "ok"}, - }, + "Example": {Value: map[string]any{"status": "ok"}}, + "Example 1": {Value: map[string]any{"status": "ok"}}, }, }}, }, @@ -284,16 +286,14 @@ func TestAddResponse(t *testing.T) { Properties: map[string]Schema{"Error": {Type: "string"}}, }, Examples: map[string]Example{ - "struct { Error string }": { - Value: struct{ Error string }{Error: "invalid request"}, - }, + "Example": {Value: struct{ Error string }{Error: "invalid request"}}, }, }}, }, }, }) if !eq { - t.Logf(diff) + t.Log(diff) t.Fail() }