Skip to content

Commit

Permalink
add support for field_behavior options on swagger plugin (#1806)
Browse files Browse the repository at this point in the history
* add support for field_behavior optons on swagger plugin

* fix lint errors

* remove extra whitespace

* add examples for google.api.field_behavior

* update examples for field_behavior

* honor camelcase for required array and add required properties to object level array

* regenerate example proto

* cleanup tests; add note on supported INPUT_ONLY at a later date

* address code review comment

* run go fmt
  • Loading branch information
ewhauser authored Nov 14, 2020
1 parent 1f6e7b0 commit 58f6deb
Show file tree
Hide file tree
Showing 15 changed files with 1,315 additions and 779 deletions.
63 changes: 63 additions & 0 deletions examples/internal/clients/abe/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ paths:
format: "int64"
x-exportParamName: "Int64OverrideType"
x-optionalDataType: "Int64"
- name: "requiredStringViaFieldBehaviorAnnotation"
in: "query"
description: "mark a field as required in Open API definition."
required: true
type: "string"
x-exportParamName: "RequiredStringViaFieldBehaviorAnnotation"
- name: "outputOnlyStringViaFieldBehaviorAnnotation"
in: "query"
description: "mark a field as readonly in Open API definition."
required: false
type: "string"
x-exportParamName: "OutputOnlyStringViaFieldBehaviorAnnotation"
x-optionalDataType: "String"
responses:
200:
description: "A successful response."
Expand Down Expand Up @@ -667,6 +680,19 @@ paths:
format: "int64"
x-exportParamName: "Int64OverrideType"
x-optionalDataType: "Int64"
- name: "requiredStringViaFieldBehaviorAnnotation"
in: "query"
description: "mark a field as required in Open API definition."
required: true
type: "string"
x-exportParamName: "RequiredStringViaFieldBehaviorAnnotation"
- name: "outputOnlyStringViaFieldBehaviorAnnotation"
in: "query"
description: "mark a field as readonly in Open API definition."
required: false
type: "string"
x-exportParamName: "OutputOnlyStringViaFieldBehaviorAnnotation"
x-optionalDataType: "String"
responses:
200:
description: "A successful response."
Expand Down Expand Up @@ -948,6 +974,19 @@ paths:
format: "int64"
x-exportParamName: "Int64OverrideType"
x-optionalDataType: "Int64"
- name: "requiredStringViaFieldBehaviorAnnotation"
in: "query"
description: "mark a field as required in Open API definition."
required: true
type: "string"
x-exportParamName: "RequiredStringViaFieldBehaviorAnnotation"
- name: "outputOnlyStringViaFieldBehaviorAnnotation"
in: "query"
description: "mark a field as readonly in Open API definition."
required: false
type: "string"
x-exportParamName: "OutputOnlyStringViaFieldBehaviorAnnotation"
x-optionalDataType: "String"
responses:
200:
description: "A successful response."
Expand Down Expand Up @@ -1249,6 +1288,19 @@ paths:
format: "int64"
x-exportParamName: "Int64OverrideType"
x-optionalDataType: "Int64"
- name: "requiredStringViaFieldBehaviorAnnotation"
in: "query"
description: "mark a field as required in Open API definition."
required: true
type: "string"
x-exportParamName: "RequiredStringViaFieldBehaviorAnnotation"
- name: "outputOnlyStringViaFieldBehaviorAnnotation"
in: "query"
description: "mark a field as readonly in Open API definition."
required: false
type: "string"
x-exportParamName: "OutputOnlyStringViaFieldBehaviorAnnotation"
x-optionalDataType: "String"
responses:
200:
description: "A successful response."
Expand Down Expand Up @@ -2340,6 +2392,10 @@ definitions:
examplepbABitOfEverything:
type: "object"
required:
- "doubleValue"
- "floatValue"
- "int64Value"
- "requiredStringViaFieldBehaviorAnnotation"
- "uuid"
properties:
singleNested:
Expand Down Expand Up @@ -2464,6 +2520,13 @@ definitions:
int64OverrideType:
type: "integer"
format: "int64"
requiredStringViaFieldBehaviorAnnotation:
type: "string"
title: "mark a field as required in Open API definition"
outputOnlyStringViaFieldBehaviorAnnotation:
type: "string"
title: "mark a field as readonly in Open API definition"
readOnly: true
externalDocs:
description: "Find out more about ABitOfEverything"
url: "https://github.com/grpc-ecosystem/grpc-gateway"
Expand Down
36 changes: 32 additions & 4 deletions examples/internal/clients/abe/api_a_bit_of_everything_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ ABitOfEverythingServiceApiService
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @param singleNestedName name is nested field.
* @param floatValue Float value field
* @param requiredStringViaFieldBehaviorAnnotation mark a field as required in Open API definition.
* @param optional nil or *ABitOfEverythingServiceCheckGetQueryParamsOpts - Optional Parameters:
* @param "SingleNestedAmount" (optional.Int64) -
* @param "SingleNestedOk" (optional.String) - DeepEnum description. - FALSE: FALSE is false. - TRUE: TRUE is true.
Expand Down Expand Up @@ -209,6 +210,7 @@ ABitOfEverythingServiceApiService
* @param "NestedAnnotationAmount" (optional.Int64) -
* @param "NestedAnnotationOk" (optional.String) - DeepEnum description. - FALSE: FALSE is false. - TRUE: TRUE is true.
* @param "Int64OverrideType" (optional.Int64) -
* @param "OutputOnlyStringViaFieldBehaviorAnnotation" (optional.String) - mark a field as readonly in Open API definition.
@return ExamplepbABitOfEverything
*/
Expand Down Expand Up @@ -245,9 +247,10 @@ type ABitOfEverythingServiceCheckGetQueryParamsOpts struct {
NestedAnnotationAmount optional.Int64
NestedAnnotationOk optional.String
Int64OverrideType optional.Int64
OutputOnlyStringViaFieldBehaviorAnnotation optional.String
}

func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckGetQueryParams(ctx context.Context, singleNestedName string, floatValue float32, localVarOptionals *ABitOfEverythingServiceCheckGetQueryParamsOpts) (ExamplepbABitOfEverything, *http.Response, error) {
func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckGetQueryParams(ctx context.Context, singleNestedName string, floatValue float32, requiredStringViaFieldBehaviorAnnotation string, localVarOptionals *ABitOfEverythingServiceCheckGetQueryParamsOpts) (ExamplepbABitOfEverything, *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Get")
localVarPostBody interface{}
Expand Down Expand Up @@ -358,6 +361,10 @@ func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckGetQuery
if localVarOptionals != nil && localVarOptionals.Int64OverrideType.IsSet() {
localVarQueryParams.Add("int64OverrideType", parameterToString(localVarOptionals.Int64OverrideType.Value(), ""))
}
localVarQueryParams.Add("requiredStringViaFieldBehaviorAnnotation", parameterToString(requiredStringViaFieldBehaviorAnnotation, ""))
if localVarOptionals != nil && localVarOptionals.OutputOnlyStringViaFieldBehaviorAnnotation.IsSet() {
localVarQueryParams.Add("outputOnlyStringViaFieldBehaviorAnnotation", parameterToString(localVarOptionals.OutputOnlyStringViaFieldBehaviorAnnotation.Value(), ""))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json", "application/x-foo-mime"}

Expand Down Expand Up @@ -484,6 +491,7 @@ ABitOfEverythingServiceApiService
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @param singleNestedOk DeepEnum description.
* @param floatValue Float value field
* @param requiredStringViaFieldBehaviorAnnotation mark a field as required in Open API definition.
* @param optional nil or *ABitOfEverythingServiceCheckNestedEnumGetQueryParamsOpts - Optional Parameters:
* @param "SingleNestedName" (optional.String) - name is nested field.
* @param "SingleNestedAmount" (optional.Int64) -
Expand Down Expand Up @@ -516,6 +524,7 @@ ABitOfEverythingServiceApiService
* @param "NestedAnnotationName" (optional.String) - name is nested field.
* @param "NestedAnnotationAmount" (optional.Int64) -
* @param "Int64OverrideType" (optional.Int64) -
* @param "OutputOnlyStringViaFieldBehaviorAnnotation" (optional.String) - mark a field as readonly in Open API definition.
@return ExamplepbABitOfEverything
*/
Expand Down Expand Up @@ -552,9 +561,10 @@ type ABitOfEverythingServiceCheckNestedEnumGetQueryParamsOpts struct {
NestedAnnotationName optional.String
NestedAnnotationAmount optional.Int64
Int64OverrideType optional.Int64
OutputOnlyStringViaFieldBehaviorAnnotation optional.String
}

func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckNestedEnumGetQueryParams(ctx context.Context, singleNestedOk string, floatValue float32, localVarOptionals *ABitOfEverythingServiceCheckNestedEnumGetQueryParamsOpts) (ExamplepbABitOfEverything, *http.Response, error) {
func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckNestedEnumGetQueryParams(ctx context.Context, singleNestedOk string, floatValue float32, requiredStringViaFieldBehaviorAnnotation string, localVarOptionals *ABitOfEverythingServiceCheckNestedEnumGetQueryParamsOpts) (ExamplepbABitOfEverything, *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Get")
localVarPostBody interface{}
Expand Down Expand Up @@ -665,6 +675,10 @@ func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckNestedEn
if localVarOptionals != nil && localVarOptionals.Int64OverrideType.IsSet() {
localVarQueryParams.Add("int64OverrideType", parameterToString(localVarOptionals.Int64OverrideType.Value(), ""))
}
localVarQueryParams.Add("requiredStringViaFieldBehaviorAnnotation", parameterToString(requiredStringViaFieldBehaviorAnnotation, ""))
if localVarOptionals != nil && localVarOptionals.OutputOnlyStringViaFieldBehaviorAnnotation.IsSet() {
localVarQueryParams.Add("outputOnlyStringViaFieldBehaviorAnnotation", parameterToString(localVarOptionals.OutputOnlyStringViaFieldBehaviorAnnotation.Value(), ""))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json", "application/x-foo-mime"}

Expand Down Expand Up @@ -792,6 +806,7 @@ ABitOfEverythingServiceApiService
* @param stringValue
* @param body
* @param floatValue Float value field
* @param requiredStringViaFieldBehaviorAnnotation mark a field as required in Open API definition.
* @param optional nil or *ABitOfEverythingServiceCheckPostQueryParamsOpts - Optional Parameters:
* @param "Uuid" (optional.String) -
* @param "DoubleValue" (optional.Float64) -
Expand Down Expand Up @@ -822,6 +837,7 @@ ABitOfEverythingServiceApiService
* @param "NestedAnnotationAmount" (optional.Int64) -
* @param "NestedAnnotationOk" (optional.String) - DeepEnum description. - FALSE: FALSE is false. - TRUE: TRUE is true.
* @param "Int64OverrideType" (optional.Int64) -
* @param "OutputOnlyStringViaFieldBehaviorAnnotation" (optional.String) - mark a field as readonly in Open API definition.
@return ExamplepbABitOfEverything
*/
Expand Down Expand Up @@ -856,9 +872,10 @@ type ABitOfEverythingServiceCheckPostQueryParamsOpts struct {
NestedAnnotationAmount optional.Int64
NestedAnnotationOk optional.String
Int64OverrideType optional.Int64
OutputOnlyStringViaFieldBehaviorAnnotation optional.String
}

func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckPostQueryParams(ctx context.Context, stringValue string, body ABitOfEverythingNested, floatValue float32, localVarOptionals *ABitOfEverythingServiceCheckPostQueryParamsOpts) (ExamplepbABitOfEverything, *http.Response, error) {
func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckPostQueryParams(ctx context.Context, stringValue string, body ABitOfEverythingNested, floatValue float32, requiredStringViaFieldBehaviorAnnotation string, localVarOptionals *ABitOfEverythingServiceCheckPostQueryParamsOpts) (ExamplepbABitOfEverything, *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Post")
localVarPostBody interface{}
Expand Down Expand Up @@ -963,6 +980,10 @@ func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceCheckPostQuer
if localVarOptionals != nil && localVarOptionals.Int64OverrideType.IsSet() {
localVarQueryParams.Add("int64OverrideType", parameterToString(localVarOptionals.Int64OverrideType.Value(), ""))
}
localVarQueryParams.Add("requiredStringViaFieldBehaviorAnnotation", parameterToString(requiredStringViaFieldBehaviorAnnotation, ""))
if localVarOptionals != nil && localVarOptionals.OutputOnlyStringViaFieldBehaviorAnnotation.IsSet() {
localVarQueryParams.Add("outputOnlyStringViaFieldBehaviorAnnotation", parameterToString(localVarOptionals.OutputOnlyStringViaFieldBehaviorAnnotation.Value(), ""))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json", "application/x-foo-mime"}

Expand Down Expand Up @@ -2304,6 +2325,7 @@ ABitOfEverythingServiceApiService
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @param uuid
* @param floatValue Float value field
* @param requiredStringViaFieldBehaviorAnnotation mark a field as required in Open API definition.
* @param optional nil or *ABitOfEverythingServiceGetQueryOpts - Optional Parameters:
* @param "SingleNestedName" (optional.String) - name is nested field.
* @param "SingleNestedAmount" (optional.Int64) -
Expand Down Expand Up @@ -2337,6 +2359,7 @@ ABitOfEverythingServiceApiService
* @param "NestedAnnotationAmount" (optional.Int64) -
* @param "NestedAnnotationOk" (optional.String) - DeepEnum description. - FALSE: FALSE is false. - TRUE: TRUE is true.
* @param "Int64OverrideType" (optional.Int64) -
* @param "OutputOnlyStringViaFieldBehaviorAnnotation" (optional.String) - mark a field as readonly in Open API definition.
@return interface{}
*/
Expand Down Expand Up @@ -2374,9 +2397,10 @@ type ABitOfEverythingServiceGetQueryOpts struct {
NestedAnnotationAmount optional.Int64
NestedAnnotationOk optional.String
Int64OverrideType optional.Int64
OutputOnlyStringViaFieldBehaviorAnnotation optional.String
}

func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceGetQuery(ctx context.Context, uuid string, floatValue float32, localVarOptionals *ABitOfEverythingServiceGetQueryOpts) (interface{}, *http.Response, error) {
func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceGetQuery(ctx context.Context, uuid string, floatValue float32, requiredStringViaFieldBehaviorAnnotation string, localVarOptionals *ABitOfEverythingServiceGetQueryOpts) (interface{}, *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Get")
localVarPostBody interface{}
Expand Down Expand Up @@ -2490,6 +2514,10 @@ func (a *ABitOfEverythingServiceApiService) ABitOfEverythingServiceGetQuery(ctx
if localVarOptionals != nil && localVarOptionals.Int64OverrideType.IsSet() {
localVarQueryParams.Add("int64OverrideType", parameterToString(localVarOptionals.Int64OverrideType.Value(), ""))
}
localVarQueryParams.Add("requiredStringViaFieldBehaviorAnnotation", parameterToString(requiredStringViaFieldBehaviorAnnotation, ""))
if localVarOptionals != nil && localVarOptionals.OutputOnlyStringViaFieldBehaviorAnnotation.IsSet() {
localVarQueryParams.Add("outputOnlyStringViaFieldBehaviorAnnotation", parameterToString(localVarOptionals.OutputOnlyStringViaFieldBehaviorAnnotation.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
Expand Up @@ -20,9 +20,9 @@ type ExamplepbABitOfEverything struct {
Uuid string `json:"uuid"`
Nested []ABitOfEverythingNested `json:"nested,omitempty"`
// Float value field
FloatValue float32 `json:"floatValue,omitempty"`
DoubleValue float64 `json:"doubleValue,omitempty"`
Int64Value string `json:"int64Value,omitempty"`
FloatValue float32 `json:"floatValue"`
DoubleValue float64 `json:"doubleValue"`
Int64Value string `json:"int64Value"`
Uint64Value string `json:"uint64Value,omitempty"`
Int32Value int32 `json:"int32Value,omitempty"`
Fixed64Value string `json:"fixed64Value,omitempty"`
Expand Down Expand Up @@ -58,4 +58,6 @@ type ExamplepbABitOfEverything struct {
// Nested object description.
NestedAnnotation *ABitOfEverythingNested `json:"nestedAnnotation,omitempty"`
Int64OverrideType int64 `json:"int64OverrideType,omitempty"`
RequiredStringViaFieldBehaviorAnnotation string `json:"requiredStringViaFieldBehaviorAnnotation"`
OutputOnlyStringViaFieldBehaviorAnnotation string `json:"outputOnlyStringViaFieldBehaviorAnnotation,omitempty"`
}
1 change: 1 addition & 0 deletions examples/internal/clients/unannotatedecho/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ definitions:
type: "object"
required:
- "id"
- "num"
properties:
id:
type: "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ExamplepbUnannotatedSimpleMessage struct {
// Id represents the message identifier.
Id string `json:"id"`
// Int value field
Num string `json:"num,omitempty"`
Num string `json:"num"`
Duration string `json:"duration,omitempty"`
LineNum string `json:"lineNum,omitempty"`
Lang string `json:"lang,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion examples/internal/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func TestUnannotatedEchoBodyClient(t *testing.T) {

cl := unannotatedecho.NewAPIClient(cfg)

req := unannotatedecho.ExamplepbUnannotatedSimpleMessage{Id: "foo"}
req := unannotatedecho.ExamplepbUnannotatedSimpleMessage{Id: "foo", Num: "1"}
resp, _, err := cl.UnannotatedEchoServiceApi.UnannotatedEchoServiceEchoBody(context.Background(), req)
if err != nil {
t.Errorf("cl.EchoBody(%#v) failed with %v; want success", req, err)
Expand Down
Loading

0 comments on commit 58f6deb

Please sign in to comment.