Skip to content

Commit

Permalink
fix path param override by query params
Browse files Browse the repository at this point in the history
  • Loading branch information
ljmsc committed Jan 29, 2024
1 parent 5a55932 commit 15e1c5b
Show file tree
Hide file tree
Showing 17 changed files with 2,206 additions and 220 deletions.
350 changes: 350 additions & 0 deletions examples/internal/clients/echo/api/swagger.yaml

Large diffs are not rendered by default.

440 changes: 440 additions & 0 deletions examples/internal/clients/echo/api_echo_service.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions examples/internal/clients/echo/model_examplepb_nested_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Echo Service
*
* Echo Service API consists of a single service which returns a message.
*
* API version: version not set
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/

package echo

type ExamplepbNestedMessage struct {
NId string `json:"nId,omitempty"`
Val string `json:"val,omitempty"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ type ExamplepbSimpleMessage struct {
Status *ExamplepbEmbedded `json:"status,omitempty"`
En string `json:"en,omitempty"`
No *ExamplepbEmbedded `json:"no,omitempty"`
ResourceId string `json:"resourceId,omitempty"`
NId *ExamplepbNestedMessage `json:"nId,omitempty"`
}
65 changes: 65 additions & 0 deletions examples/internal/clients/unannotatedecho/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ paths:
type: "string"
x-exportParamName: "NoNote"
x-optionalDataType: "String"
- name: "resourceId"
in: "query"
required: false
type: "string"
x-exportParamName: "ResourceId"
x-optionalDataType: "String"
- name: "nId.nId"
in: "query"
required: false
type: "string"
x-exportParamName: "NIdNId"
x-optionalDataType: "String"
- name: "nId.val"
in: "query"
required: false
type: "string"
x-exportParamName: "NIdVal"
x-optionalDataType: "String"
responses:
200:
description: "A successful response."
Expand Down Expand Up @@ -221,6 +239,24 @@ paths:
type: "string"
x-exportParamName: "NoNote"
x-optionalDataType: "String"
- name: "resourceId"
in: "query"
required: false
type: "string"
x-exportParamName: "ResourceId"
x-optionalDataType: "String"
- name: "nId.nId"
in: "query"
required: false
type: "string"
x-exportParamName: "NIdNId"
x-optionalDataType: "String"
- name: "nId.val"
in: "query"
required: false
type: "string"
x-exportParamName: "NIdVal"
x-optionalDataType: "String"
responses:
200:
description: "A successful response."
Expand Down Expand Up @@ -355,6 +391,24 @@ paths:
type: "string"
x-exportParamName: "NoNote"
x-optionalDataType: "String"
- name: "resourceId"
in: "query"
required: false
type: "string"
x-exportParamName: "ResourceId"
x-optionalDataType: "String"
- name: "nId.nId"
in: "query"
required: false
type: "string"
x-exportParamName: "NIdNId"
x-optionalDataType: "String"
- name: "nId.val"
in: "query"
required: false
type: "string"
x-exportParamName: "NIdVal"
x-optionalDataType: "String"
responses:
200:
description: "A successful response."
Expand Down Expand Up @@ -394,6 +448,13 @@ definitions:
note:
type: "string"
description: "Embedded represents a message embedded in SimpleMessage."
examplepbUnannotatedNestedMessage:
type: "object"
properties:
nId:
type: "string"
val:
type: "string"
examplepbUnannotatedSimpleMessage:
type: "object"
required:
Expand Down Expand Up @@ -422,6 +483,10 @@ definitions:
format: "int64"
"no":
$ref: "#/definitions/examplepbUnannotatedEmbedded"
resourceId:
type: "string"
nId:
$ref: "#/definitions/examplepbUnannotatedNestedMessage"
externalDocs:
description: "Find out more about UnannotatedSimpleMessage"
url: "https://github.com/grpc-ecosystem/grpc-gateway"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Description Echo
* @param "En" (optional.String) -
* @param "NoProgress" (optional.String) -
* @param "NoNote" (optional.String) -
* @param "ResourceId" (optional.String) -
* @param "NIdNId" (optional.String) -
* @param "NIdVal" (optional.String) -
@return ExamplepbUnannotatedSimpleMessage
*/
Expand All @@ -55,6 +58,9 @@ type UnannotatedEchoServiceEchoOpts struct {
En optional.String
NoProgress optional.String
NoNote optional.String
ResourceId optional.String
NIdNId optional.String
NIdVal optional.String
}

func (a *UnannotatedEchoServiceApiService) UnannotatedEchoServiceEcho(ctx context.Context, id string, num string, localVarOptionals *UnannotatedEchoServiceEchoOpts) (ExamplepbUnannotatedSimpleMessage, *http.Response, error) {
Expand Down Expand Up @@ -99,6 +105,15 @@ func (a *UnannotatedEchoServiceApiService) UnannotatedEchoServiceEcho(ctx contex
if localVarOptionals != nil && localVarOptionals.NoNote.IsSet() {
localVarQueryParams.Add("no.note", parameterToString(localVarOptionals.NoNote.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.ResourceId.IsSet() {
localVarQueryParams.Add("resourceId", parameterToString(localVarOptionals.ResourceId.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NIdNId.IsSet() {
localVarQueryParams.Add("nId.nId", parameterToString(localVarOptionals.NIdNId.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NIdVal.IsSet() {
localVarQueryParams.Add("nId.val", parameterToString(localVarOptionals.NIdVal.Value(), ""))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json", "application/x-foo-mime"}

Expand Down Expand Up @@ -235,6 +250,9 @@ Description Echo
* @param "En" (optional.String) -
* @param "NoProgress" (optional.String) -
* @param "NoNote" (optional.String) -
* @param "ResourceId" (optional.String) -
* @param "NIdNId" (optional.String) -
* @param "NIdVal" (optional.String) -
@return ExamplepbUnannotatedSimpleMessage
*/
Expand All @@ -248,6 +266,9 @@ type UnannotatedEchoServiceEcho2Opts struct {
En optional.String
NoProgress optional.String
NoNote optional.String
ResourceId optional.String
NIdNId optional.String
NIdVal optional.String
}

func (a *UnannotatedEchoServiceApiService) UnannotatedEchoServiceEcho2(ctx context.Context, id string, num string, localVarOptionals *UnannotatedEchoServiceEcho2Opts) (ExamplepbUnannotatedSimpleMessage, *http.Response, error) {
Expand Down Expand Up @@ -292,6 +313,15 @@ func (a *UnannotatedEchoServiceApiService) UnannotatedEchoServiceEcho2(ctx conte
if localVarOptionals != nil && localVarOptionals.NoNote.IsSet() {
localVarQueryParams.Add("no.note", parameterToString(localVarOptionals.NoNote.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.ResourceId.IsSet() {
localVarQueryParams.Add("resourceId", parameterToString(localVarOptionals.ResourceId.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NIdNId.IsSet() {
localVarQueryParams.Add("nId.nId", parameterToString(localVarOptionals.NIdNId.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NIdVal.IsSet() {
localVarQueryParams.Add("nId.val", parameterToString(localVarOptionals.NIdVal.Value(), ""))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json", "application/x-foo-mime"}

Expand Down Expand Up @@ -562,6 +592,9 @@ UnannotatedEchoServiceApiService EchoDelete method receives a simple message and
* @param "En" (optional.String) -
* @param "NoProgress" (optional.String) -
* @param "NoNote" (optional.String) -
* @param "ResourceId" (optional.String) -
* @param "NIdNId" (optional.String) -
* @param "NIdVal" (optional.String) -
@return ExamplepbUnannotatedSimpleMessage
*/
Expand All @@ -576,6 +609,9 @@ type UnannotatedEchoServiceEchoDeleteOpts struct {
En optional.String
NoProgress optional.String
NoNote optional.String
ResourceId optional.String
NIdNId optional.String
NIdVal optional.String
}

func (a *UnannotatedEchoServiceApiService) UnannotatedEchoServiceEchoDelete(ctx context.Context, num string, localVarOptionals *UnannotatedEchoServiceEchoDeleteOpts) (ExamplepbUnannotatedSimpleMessage, *http.Response, error) {
Expand Down Expand Up @@ -622,6 +658,15 @@ func (a *UnannotatedEchoServiceApiService) UnannotatedEchoServiceEchoDelete(ctx
if localVarOptionals != nil && localVarOptionals.NoNote.IsSet() {
localVarQueryParams.Add("no.note", parameterToString(localVarOptionals.NoNote.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.ResourceId.IsSet() {
localVarQueryParams.Add("resourceId", parameterToString(localVarOptionals.ResourceId.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NIdNId.IsSet() {
localVarQueryParams.Add("nId.nId", parameterToString(localVarOptionals.NIdNId.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NIdVal.IsSet() {
localVarQueryParams.Add("nId.val", parameterToString(localVarOptionals.NIdVal.Value(), ""))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json", "application/x-foo-mime"}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Unannotated Echo
*
* Unannotated Echo Service Similar to echo_service.proto but without annotations. See unannotated_echo_service.yaml for the equivalent of the annotations in gRPC API configuration format. Echo Service API consists of a single service which returns a message.
*
* API version: 1.0
* Contact: [email protected]
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/

package unannotatedecho

type ExamplepbUnannotatedNestedMessage struct {
NId string `json:"nId,omitempty"`
Val string `json:"val,omitempty"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ type ExamplepbUnannotatedSimpleMessage struct {
Status *ExamplepbUnannotatedEmbedded `json:"status,omitempty"`
En string `json:"en,omitempty"`
No *ExamplepbUnannotatedEmbedded `json:"no,omitempty"`
ResourceId string `json:"resourceId,omitempty"`
NId *ExamplepbUnannotatedNestedMessage `json:"nId,omitempty"`
}
88 changes: 88 additions & 0 deletions examples/internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func TestEcho(t *testing.T) {
testEchoOneof(t, 8088, apiPrefix, "application/json")
testEchoOneof1(t, 8088, apiPrefix, "application/json")
testEchoOneof2(t, 8088, apiPrefix, "application/json")
testEchoPathParamOverwrite(t, 8088)
testEchoNested(t, 8088)
testEchoNestedOverride(t, 8088)
testEchoBody(t, 8088, apiPrefix, true)
testEchoBody(t, 8088, apiPrefix, false)
// Use SendHeader/SetTrailer without gRPC server https://github.com/grpc-ecosystem/grpc-gateway/issues/517#issuecomment-684625645
Expand Down Expand Up @@ -349,6 +352,91 @@ func testEchoOneof2(t *testing.T, port int, apiPrefix string, contentType string
}
}

func testEchoPathParamOverwrite(t *testing.T, port int) {
apiURL := fmt.Sprintf("http://localhost:%d/v1/example/echo/resource/my_resource_id?resourceId=bad_resource_id", port)
resp, err := http.Get(apiURL)
if err != nil {
t.Errorf("http.Get(%q) failed with %v; want success", apiURL, err)
return
}
defer resp.Body.Close()
buf, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err)
return
}

if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("resp.StatusCode = %d; want %d", got, want)
t.Logf("%s", buf)
}

msg := new(examplepb.UnannotatedSimpleMessage)
if err := marshaler.Unmarshal(buf, msg); err != nil {
t.Errorf("marshaler.Unmarshal(%s, msg) failed with %v; want success", buf, err)
return
}
if got, want := msg.GetResourceId(), "my_resource_id"; got != want {
t.Errorf("msg.GetResourceId() = %q; want %q", got, want)
}
}

func testEchoNested(t *testing.T, port int) {
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/v1/example/echo/nested/my_nested_id?n_id.val=foo", port))
if err != nil {
t.Errorf("http.Get() failed with %v; want success", err)
return
}
defer resp.Body.Close()
buf, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err)
return
}

if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("resp.StatusCode = %d; want %d", got, want)
t.Logf("%s", buf)
}

msg := new(examplepb.UnannotatedSimpleMessage)
if err := marshaler.Unmarshal(buf, msg); err != nil {
t.Errorf("marshaler.Unmarshal(%s, msg) failed with %v; want success", buf, err)
return
}
if got, want := msg.NId.Val, "foo"; got != want {
t.Errorf("msg.NId.Val = %q; want %q", got, want)
}
}

func testEchoNestedOverride(t *testing.T, port int) {
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/v1/example/echo/nested/my_nested_id?nId.nId=bad_id", port))
if err != nil {
t.Errorf("http.Get() failed with %v; want success", err)
return
}
defer resp.Body.Close()
buf, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("io.ReadAll(resp.Body) failed with %v; want success", err)
return
}

if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("resp.StatusCode = %d; want %d", got, want)
t.Logf("%s", buf)
}

msg := new(examplepb.UnannotatedSimpleMessage)
if err := marshaler.Unmarshal(buf, msg); err != nil {
t.Errorf("marshaler.Unmarshal(%s, msg) failed with %v; want success", buf, err)
return
}
if got, want := msg.NId.NId, "my_nested_id"; got != want {
t.Errorf("msg.NId.NId = %q; want %q", got, want)
}
}

func testEchoBody(t *testing.T, port int, apiPrefix string, useTrailers bool) {
sent := examplepb.UnannotatedSimpleMessage{Id: "example"}
payload, err := marshaler.Marshal(&sent)
Expand Down
Loading

0 comments on commit 15e1c5b

Please sign in to comment.