Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand Down
36 changes: 24 additions & 12 deletions paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -197,28 +205,28 @@ 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)
}
schema := buildSchema(i)
if m.Schema.Title == "" {
m.Schema = schema
}
if exName == "" {
exName = schema.Title
if name == "" {
name = "Example"
}
ex := Example{
Desc: schema.Desc,
Value: i,
}

// 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.
Expand All @@ -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] == ']' {
Expand Down
20 changes: 10 additions & 10 deletions paths_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package openapi

import (
"github.com/hydronica/trial"
"testing"

"github.com/hydronica/trial"
)

func TestAddParams(t *testing.T) {
Expand Down Expand Up @@ -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",
Expand All @@ -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"}},
},
}},
},
Expand All @@ -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()
}

Expand Down