Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: spec add examples #32

Merged
merged 2 commits into from
Nov 16, 2023
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
44 changes: 42 additions & 2 deletions backend/common/spec/plugin/openapi/2.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,29 @@ func (s *fromSwagger) parseResponsesDefine(in *v2.Swagger) []spec.HTTPResponseDe
}
if len(in.Produces) == 0 {
content[defaultSwaggerConsumerProduce] = sh
if res.Examples != nil {
sh.Examples = make(map[string]spec.Example)
for k, v := range res.Examples.Values {
sh.Examples[k] = spec.Example{
Summary: "example",
Value: v,
}
}
}
} else {
for _, v := range in.Produces {
content[v] = sh
if res.Examples != nil {
emp, ok := res.Examples.Values[v]
if ok {
sh.Examples = map[string]spec.Example{
v: {
Summary: "example",
Value: emp,
},
}
}
}
}
}
}
Expand Down Expand Up @@ -252,9 +272,21 @@ func (s *fromSwagger) parseResponse(info *v2.Operation) *spec.HTTPResponsesNode
if res.Schema != nil {
js := s.parseContent(res.Schema)
for _, v := range info.Produces {
resp.Content[v] = &spec.Schema{
sh := &spec.Schema{
Schema: js,
}
if res.Examples != nil {
mp, ok := res.Examples.Values[v]
if ok {
sh.Examples = map[string]spec.Example{
v: {
Summary: "example",
Value: mp,
},
}
}
}
resp.Content[v] = sh
}
}
outresponses.List = append(outresponses.List, resp)
Expand Down Expand Up @@ -505,8 +537,16 @@ func (s *toSwagger) parseResponse(in *spec.Spec, res spec.HTTPResponseDefine) ma
resp["headers"] = h
}
if res.Content != nil {
for _, v := range res.Content {
for k, v := range res.Content {
resp["schema"] = s.convertJSONSchema(v.Schema)
if v.Examples != nil {
for _, v := range v.Examples {
resp["examples"] = map[string]any{
k: v,
}
break
}
}
break
}
}
Expand Down
14 changes: 14 additions & 0 deletions backend/common/spec/plugin/openapi/3.x.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ func (o *fromOpenapi) parseContent(mts map[string]*v3.MediaType) spec.HTTPBody {
panic(err)
}
js.Example = mt.Example
// sh.Example = mt.Example
if len(mt.Examples) > 0 {
sh.Examples = make(map[string]spec.Example)
for k, v := range mt.Examples {
sh.Examples[k] = spec.Example{
Summary: v.Summary,
Value: v.Value,
}
}
}
sh.Schema = js
content[contentType] = sh
}
Expand Down Expand Up @@ -386,6 +396,10 @@ func (o *toOpenapi) toPaths(ver string, in *spec.Spec) (
sp := &spec.Schema{
Schema: o.convertJSONSchema(ver, v.Schema),
Description: v.Description,
Examples: v.Examples,
}
if sp.Schema.Example != nil {
sp.Example = sp.Schema.Example
}
if item.RequestBody == nil {
item.RequestBody = &openapiRequestbody{
Expand Down
73 changes: 37 additions & 36 deletions backend/common/spec/plugin/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,45 @@ import (
"github.com/apicat/apicat/backend/common/spec"
)

// func TestDecode(t *testing.T) {
// fs := map[string]string{
// // "swagger": "../../testdata/swagger.json",
// // "openapi3.0": "../../testdata/openapi3.0.yaml",
// "openapi3.1": "../../testdata/openapi3.1.yaml",
// }
// for k, v := range fs {
// raw, err := os.ReadFile(v)
// if err != nil {
// t.Fatal(k, err)
// }
// if x, err := Decode(raw); err != nil {
// t.Fatal(k, err)
// } else {
// d, _ := x.ToJSON(spec.JSONOption{Indent: ""})
// fmt.Println(string(d))
// }
// }
// }

func TestEncode(t *testing.T) {
raw, err := os.ReadFile("../../testdata/spec.json")
if err != nil {
t.FailNow()
}
p, err := spec.ParseJSON(raw)
if err != nil {
t.FailNow()
func TestDecode(t *testing.T) {
fs := map[string]string{
// "swagger": "../../testdata/swagger.json",
// "openapi3.0": "../../testdata/openapi3.0.yaml",
// "openapi3.1": "../../testdata/openapi3.1.yaml",
"openpai": "../../testdata/openapi3-examples.json",
}

fmt.Printf("%+v\n", p)

for _, v := range []string{"3.1.0"} {
p, err := Encode(p, v)
for k, v := range fs {
raw, err := os.ReadFile(v)
if err != nil {
t.Fatal(v, err)
t.Fatal(k, err)
}
if x, err := Decode(raw); err != nil {
t.Fatal(k, err)
} else {
d, _ := x.ToJSON(spec.JSONOption{Indent: ""})
fmt.Println(string(d))
}
fmt.Println(string(p))
}

}

// func TestEncode(t *testing.T) {
// raw, err := os.ReadFile("../../testdata/spec.json")
// if err != nil {
// t.FailNow()
// }
// p, err := spec.ParseJSON(raw)
// if err != nil {
// t.FailNow()
// }

// fmt.Printf("%+v\n", p)

// for _, v := range []string{"3.1.0"} {
// p, err := Encode(p, v)
// if err != nil {
// t.Fatal(v, err)
// }
// fmt.Println(string(p))
// }

// }
6 changes: 6 additions & 0 deletions backend/common/spec/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ type Schema struct {
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Example any `json:"example,omitempty"`
Examples map[string]Example `json:"examples,omitempty"`
Schema *jsonschema.Schema `json:"schema,omitempty"`
Reference *string `json:"$ref,omitempty"`
XDiff *string `json:"x-apicat-diff,omitempty"`
}

type Example struct {
Summary string `json:"summary"`
Value any `json:"value"`
}

func (s *Schema) Ref() bool { return s.Reference != nil }

type Schemas []*Schema
Expand Down
Loading
Loading