Skip to content

Commit

Permalink
fix: handle slices of pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
e-nikolov committed Feb 12, 2022
1 parent 88fe433 commit 56497bf
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions openapi2/openapi2.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ func typeName(i any) string {
}

s := strings.ReplaceAll(t.String(), " ", "")
s = strings.ReplaceAll(s, "*", "")

if s == "error" {
return "string"
Expand Down
28 changes: 28 additions & 0 deletions openapi2/openapi2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,34 @@ func TestDocs(t *testing.T) {
filePath: "testdata/t1.json",
wantErr: false,
},
{
name: "t2",
args: args{
routes: []*Route{
{
Method: "GET",
Path: "/test1/{p1}/{p2}",
Params: []spec.Parameter{
{ParamProps: spec.ParamProps{Name: "p1", In: "path", Description: "d1", Required: true}},
{ParamProps: spec.ParamProps{Name: "p2", In: "path", Description: "d2", Required: true}},
},

// ShowBottles godoc
// @Summary Test Handler
// @Description get string by ID
// @ID get-string-by-int
// @Tags bottles
// @Success 200
// @Failure 400,404,500
Handler: chai.NewReqResHandler(func(req []*tests.TestRequest, w http.ResponseWriter, r *http.Request) ([]*tests.TestResponse, int, error) {
return nil, 0, nil
}),
},
},
},
filePath: "testdata/t2.json",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
117 changes: 117 additions & 0 deletions openapi2/testdata/t2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"info": {
"contact": {}
},
"paths": {
"/test1/{p1}/{p2}": {
"get": {
"description": "get string by ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"bottles"
],
"summary": "Test Handler",
"operationId": "get-string-by-int",
"parameters": [
{
"name": "body",
"in": "body",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/tests.TestRequest"
}
}
},
{
"description": "d1",
"name": "p1",
"in": "path",
"required": true
},
{
"description": "d2",
"name": "p2",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/tests.TestResponse"
}
}
},
"400": {
"description": "",
"schema": {
"type": "string"
}
},
"404": {
"description": "",
"schema": {
"type": "string"
}
},
"500": {
"description": "",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
"tests.TestInnerResponse": {
"type": "object",
"properties": {
"bar_bar": {
"type": "integer"
},
"foo_foo": {
"type": "integer"
}
}
},
"tests.TestRequest": {
"type": "object",
"properties": {
"barb": {
"type": "string"
},
"foob": {
"type": "string"
},
"test_inner_responseb": {
"$ref": "#/definitions/tests.TestInnerResponse"
}
}
},
"tests.TestResponse": {
"type": "object",
"properties": {
"bar": {
"type": "string"
},
"foo": {
"type": "string"
},
"test_inner_response": {
"$ref": "#/definitions/tests.TestInnerResponse"
}
}
}
}
}

0 comments on commit 56497bf

Please sign in to comment.