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

Extend google.protobuf.EnumOptions #2855

Closed
wants to merge 7 commits into from
Closed
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
32 changes: 31 additions & 1 deletion docs/docs/mapping/customizing_openapi_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ message MyMessage {

## Using proto options

You can define options on your Protocol Buffer services, operations, messages, and field definitions to customize your Open API output. For instance, to customize the [OpenAPI Schema Object](https://swagger.io/specification/v2/#schemaObject) for messages and fields:
You can define options on your Protocol Buffer services, operations, messages, enums and field definitions to customize your Open API output. For instance, to customize the [OpenAPI Schema Object](https://swagger.io/specification/v2/#schemaObject) for messages and fields:

```protobuf
import "protoc-gen-openapiv2/options/annotations.proto";
Expand Down Expand Up @@ -51,6 +51,35 @@ message ABitOfEverything {
}
```

Enums can be customized like messages:

```protobuf
// NumericEnum is one or zero.
enum NumericEnum {
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_enum) = {
json_schema: {
title: "NumericEnum"
description: "NumericEnum is one or zero."
extensions: {
key: "x-a-bit-of-everything-foo";
value {
string_value: "bar";
}
}
}
external_docs: {
url: "https://github.com/grpc-ecosystem/grpc-gateway";
description: "Find out more about ABitOfEverything";
}
example: "\"ZERO\""
};
// ZERO means 0
ZERO = 0;
// ONE means 1
ONE = 1;
}
```

Operations can also be customized:

```protobuf
Expand Down Expand Up @@ -579,3 +608,4 @@ definitions:
```

{% endraw %}

9 changes: 7 additions & 2 deletions examples/internal/clients/abe/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4544,12 +4544,17 @@ definitions:
$ref: "#/definitions/examplepbErrorObject"
examplepbNumericEnum:
type: "string"
description: "NumericEnum is one or zero.\n\n - ZERO: ZERO means 0\n - ONE: ONE\
\ means 1"
externalDocs:
description: "Find out more about ABitOfEverything"
url: "https://github.com/grpc-ecosystem/grpc-gateway"
title: "NumericEnum"
description: "NumericEnum is one or zero."
example: "ZERO"
enum:
- "ZERO"
- "ONE"
default: "ZERO"
x-a-bit-of-everything-foo: "bar"
examplepbSnakeEnumResponse:
type: "object"
examplepbsnake_case_0_enum:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

package abe
// ExamplepbNumericEnum : NumericEnum is one or zero. - ZERO: ZERO means 0 - ONE: ONE means 1
// ExamplepbNumericEnum : NumericEnum is one or zero.
type ExamplepbNumericEnum string

// List of examplepbNumericEnum
Expand Down
1,012 changes: 513 additions & 499 deletions examples/internal/proto/examplepb/a_bit_of_everything.pb.go

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions examples/internal/proto/examplepb/a_bit_of_everything.proto
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,28 @@ message MessageWithBody {

// NumericEnum is one or zero.
enum NumericEnum {
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_enum) = {
json_schema: {
title: "NumericEnum"
description: "NumericEnum is one or zero."
required: [
"uuid",
"int64_value",
"double_value"
]
extensions: {
key: "x-a-bit-of-everything-foo";
value {
string_value: "bar";
}
}
}
external_docs: {
url: "https://github.com/grpc-ecosystem/grpc-gateway";
description: "Find out more about ABitOfEverything";
}
example: "\"ZERO\""
};
// ZERO means 0
ZERO = 0;
// ONE means 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6485,12 +6485,24 @@
},
"examplepbNumericEnum": {
"type": "string",
"example": "\"ZERO\"",
"enum": [
"ZERO",
"ONE"
],
"default": "ZERO",
"description": "NumericEnum is one or zero.\n\n - ZERO: ZERO means 0\n - ONE: ONE means 1"
"description": "NumericEnum is one or zero.",
"title": "NumericEnum",
"externalDocs": {
"description": "Find out more about ABitOfEverything",
"url": "https://github.com/grpc-ecosystem/grpc-gateway"
},
"required": [
"uuid",
"int64Value",
"doubleValue"
],
Comment on lines +6500 to +6504
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right, this should only apply to the messages that contain these fields. A bug?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be worthwhile for you to rebase on main, we just merged a PR that did some work around required fields.

"x-a-bit-of-everything-foo": "bar"
},
"examplepbSnakeEnumResponse": {
"type": "object"
Expand Down
14 changes: 13 additions & 1 deletion examples/internal/proto/examplepb/generated_input.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,24 @@
},
"examplepbNumericEnum": {
"type": "string",
"example": "\"ZERO\"",
"enum": [
"ZERO",
"ONE"
],
"default": "ZERO",
"description": "NumericEnum is one or zero.\n\n - ZERO: ZERO means 0\n - ONE: ONE means 1"
"description": "NumericEnum is one or zero.",
"title": "NumericEnum",
"externalDocs": {
"description": "Find out more about ABitOfEverything",
"url": "https://github.com/grpc-ecosystem/grpc-gateway"
},
"required": [
"uuid",
"int64Value",
"doubleValue"
],
"x-a-bit-of-everything-foo": "bar"
},
"pathenumPathEnum": {
"type": "string",
Expand Down
14 changes: 13 additions & 1 deletion examples/internal/proto/examplepb/stream.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,24 @@
},
"examplepbNumericEnum": {
"type": "string",
"example": "\"ZERO\"",
"enum": [
"ZERO",
"ONE"
],
"default": "ZERO",
"description": "NumericEnum is one or zero.\n\n - ZERO: ZERO means 0\n - ONE: ONE means 1"
"description": "NumericEnum is one or zero.",
"title": "NumericEnum",
"externalDocs": {
"description": "Find out more about ABitOfEverything",
"url": "https://github.com/grpc-ecosystem/grpc-gateway"
},
"required": [
"uuid",
"int64Value",
"doubleValue"
],
"x-a-bit-of-everything-foo": "bar"
},
"pathenumPathEnum": {
"type": "string",
Expand Down
52 changes: 48 additions & 4 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ func renderEnumerationsAsDefinition(enums enumMap, d openapiDefinitionsObject, r
if err := updateOpenAPIDataFromComments(reg, &enumSchemaObject, enum, enumComments, false); err != nil {
panic(err)
}
if schema, err := getEnumSchemaOption(reg, enum.EnumDescriptorProto); err == nil {
updateswaggerObjectFromSchema(&enumSchemaObject, schema, reg, nil)
}

d[swgName] = enumSchemaObject
}
Expand Down Expand Up @@ -2394,6 +2397,21 @@ func extractFieldBehaviorFromFieldDescriptor(fd *descriptorpb.FieldDescriptorPro
return opts, nil
}

func extractEnumSchemaFromEnumDescriptor(enum *descriptorpb.EnumDescriptorProto) (*openapi_options.Schema, error) {
if enum.Options == nil {
return nil, nil
}
if !proto.HasExtension(enum.Options, openapi_options.E_Openapiv2Enum) {
return nil, nil
}
ext := proto.GetExtension(enum.Options, openapi_options.E_Openapiv2Enum)
opts, ok := ext.(*openapi_options.Schema)
if !ok {
return nil, fmt.Errorf("extension is %T; want a openapi_options.Schema object", ext)
}
return opts, nil
}

func getFieldVisibilityOption(fd *descriptor.Field) *visibility.VisibilityRule {
if fd.Options == nil {
return nil
Expand Down Expand Up @@ -2525,6 +2543,17 @@ func getFieldBehaviorOption(reg *descriptor.Registry, fd *descriptor.Field) ([]a
return opts, nil
}

func getEnumSchemaOption(reg *descriptor.Registry, enum *descriptorpb.EnumDescriptorProto) (*openapi_options.Schema, error) {
opts, err := extractEnumSchemaFromEnumDescriptor(enum)
if err != nil {
return nil, err
}
if opts != nil {
return opts, nil
}
return opts, nil
}

func protoJSONSchemaToOpenAPISchemaCore(j *openapi_options.JSONSchema, reg *descriptor.Registry, refs refMap) schemaCore {
ret := schemaCore{}

Expand All @@ -2549,7 +2578,9 @@ func protoJSONSchemaToOpenAPISchemaCore(j *openapi_options.JSONSchema, reg *desc

func updateswaggerObjectFromJSONSchema(s *openapiSchemaObject, j *openapi_options.JSONSchema, reg *descriptor.Registry, data interface{}) {
s.Title = j.GetTitle()
s.Description = j.GetDescription()
if j.GetDescription() != "" {
s.Description = j.GetDescription()
}
if reg.GetUseGoTemplate() {
s.Title = goTemplateComments(s.Title, data, reg)
s.Description = goTemplateComments(s.Description, data, reg)
Expand All @@ -2574,7 +2605,6 @@ func updateswaggerObjectFromJSONSchema(s *openapiSchemaObject, j *openapi_option
s.MaxLength = j.GetMaxLength()
s.MinLength = j.GetMinLength()
s.Pattern = j.GetPattern()
s.Default = j.GetDefault()
s.UniqueItems = j.GetUniqueItems()
s.MaxProperties = j.GetMaxProperties()
s.MinProperties = j.GetMinProperties()
Expand All @@ -2585,7 +2615,6 @@ func updateswaggerObjectFromJSONSchema(s *openapiSchemaObject, j *openapi_option
s.MultipleOf = j.GetMultipleOf()
s.ExclusiveMaximum = j.GetExclusiveMaximum()
s.ExclusiveMinimum = j.GetExclusiveMinimum()
s.Enum = j.GetEnum()
}
s.MaxItems = j.GetMaxItems()
s.MinItems = j.GetMinItems()
Expand All @@ -2596,7 +2625,12 @@ func updateswaggerObjectFromJSONSchema(s *openapiSchemaObject, j *openapi_option
s.Required[i] = casing.JSONCamelCase(r)
}
}
s.Enum = j.GetEnum()
if j.GetDefault() != "" {
s.Default = j.GetDefault()
}
if len(j.GetEnum()) != 0 {
s.Enum = j.GetEnum()
}
if j.GetExtensions() != nil {
exts, err := processExtensions(j.GetExtensions())
if err != nil {
Expand All @@ -2615,6 +2649,16 @@ func updateswaggerObjectFromJSONSchema(s *openapiSchemaObject, j *openapi_option
}
}

func updateswaggerObjectFromSchema(s *openapiSchemaObject, j *openapi_options.Schema, reg *descriptor.Registry, data interface{}) {
if j.GetJsonSchema() != nil {
updateswaggerObjectFromJSONSchema(s, j.GetJsonSchema(), reg, data)
}
if j.GetExample() != "" { // avoid overwrite example from JsonSchema with empty string
s.Example = RawExample(strconv.Quote(j.GetExample()))
}
s.ExternalDocs = protoExternalDocumentationToOpenAPIExternalDocumentation(j.GetExternalDocs(), reg, data)
}

func updateSwaggerObjectFromFieldBehavior(s *openapiSchemaObject, j []annotations.FieldBehavior, reg *descriptor.Registry, field *descriptor.Field) {
// Per the JSON Reference syntax: Any members other than "$ref" in a JSON Reference object SHALL be ignored.
// https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03#section-3
Expand Down
Loading