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(protoc-gen-openapi): support query field behavior with required #439

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ paths:
- name: name
in: query
description: The name of the book to update.
required: true
schema:
type: string
requestBody:
Expand Down
26 changes: 20 additions & 6 deletions cmd/protoc-gen-openapi/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ func (g *OpenAPIv3Generator) _buildQueryParamsV3(field *protogen.Field, depths m
queryFieldName := g.reflect.formatFieldName(field.Desc)
fieldDescription := g.filterCommentString(field.Comments.Leading)

required := false
extension := proto.GetExtension(field.Desc.Options(), annotations.E_FieldBehavior)
if extension != nil {
fieldBehaviors, ok := extension.([]annotations.FieldBehavior)
if ok {
for _, fieldBehavior := range fieldBehaviors {
if fieldBehavior == annotations.FieldBehavior_REQUIRED {
required = true
break
}
}
}
}

if field.Desc.IsMap() {
// Map types are not allowed in query parameteres
return parameters
Expand All @@ -310,7 +324,7 @@ func (g *OpenAPIv3Generator) _buildQueryParamsV3(field *protogen.Field, depths m
Name: queryFieldName,
In: "query",
Description: fieldDescription,
Required: false,
Required: required,
Schema: fieldSchema,
},
},
Expand All @@ -329,7 +343,7 @@ func (g *OpenAPIv3Generator) _buildQueryParamsV3(field *protogen.Field, depths m
Name: queryFieldName,
In: "query",
Description: fieldDescription,
Required: false,
Required: required,
Schema: fieldSchema,
},
},
Expand All @@ -345,7 +359,7 @@ func (g *OpenAPIv3Generator) _buildQueryParamsV3(field *protogen.Field, depths m
Name: queryFieldName,
In: "query",
Description: fieldDescription,
Required: false,
Required: required,
Schema: fieldSchema,
},
},
Expand All @@ -360,7 +374,7 @@ func (g *OpenAPIv3Generator) _buildQueryParamsV3(field *protogen.Field, depths m
Name: queryFieldName,
In: "query",
Description: fieldDescription,
Required: false,
Required: required,
Schema: fieldSchema,
},
},
Expand All @@ -383,7 +397,7 @@ func (g *OpenAPIv3Generator) _buildQueryParamsV3(field *protogen.Field, depths m
Name: queryFieldName,
In: "query",
Description: fieldDescription,
Required: false,
Required: required,
Schema: fieldSchema,
},
},
Expand Down Expand Up @@ -423,7 +437,7 @@ func (g *OpenAPIv3Generator) _buildQueryParamsV3(field *protogen.Field, depths m
Name: queryFieldName,
In: "query",
Description: fieldDescription,
Required: false,
Required: required,
Schema: fieldSchema,
},
},
Expand Down