Skip to content

Commit

Permalink
create new type EnumSchema for enum options
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Hoehl <[email protected]>
  • Loading branch information
hown3d committed Nov 21, 2024
1 parent c6050c3 commit bb30cb9
Show file tree
Hide file tree
Showing 7 changed files with 1,131 additions and 904 deletions.
1,082 changes: 541 additions & 541 deletions examples/internal/proto/examplepb/a_bit_of_everything.pb.go

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions examples/internal/proto/examplepb/a_bit_of_everything.proto
Original file line number Diff line number Diff line change
Expand Up @@ -420,19 +420,17 @@ 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."
extensions: {
key: "x-a-bit-of-everything-foo";
value {
string_value: "bar";
}
description: "NumericEnum is one or zero."
title: "NumericEnum"
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";
url: "https://github.com/grpc-ecosystem/grpc-gateway"
description: "Find out more about ABitOfEverything"
}
example: "\"ZERO\""
};
Expand Down
34 changes: 26 additions & 8 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ func renderEnumerationsAsDefinition(enums enumMap, d openapiDefinitionsObject, r
panic(err)
}
if opts != nil {
protoSchema := openapiSchemaFromProtoSchema(opts, reg, customRefs, enum)
protoSchema := openapiSchemaFromProtoEnumSchema(opts, reg, customRefs, enum)
// Warning: Make sure not to overwrite any fields already set on the schema type.
// This is only a subset of the fields from JsonSchema since most of them only apply to arrays or objects not enums
enumSchemaObject.ExternalDocs = protoSchema.ExternalDocs
Expand Down Expand Up @@ -2819,19 +2819,19 @@ func extractSchemaOptionFromMessageDescriptor(msg *descriptorpb.DescriptorProto)
return opts, nil
}

// extractSchemaOptionFromEnumDescriptor extracts the message of type
// openapi_options.Schema from a given proto enum's descriptor.
func extractSchemaOptionFromEnumDescriptor(enum *descriptorpb.EnumDescriptorProto) (*openapi_options.Schema, error) {
// extractEnumSchemaOptionFromEnumDescriptor extracts the message of type
// openapi_options.EnumSchema from a given proto enum's descriptor.
func extractEnumSchemaOptionFromEnumDescriptor(enum *descriptorpb.EnumDescriptorProto) (*openapi_options.EnumSchema, 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)
opts, ok := ext.(*openapi_options.EnumSchema)
if !ok {
return nil, fmt.Errorf("extension is %T; want a Schema", ext)
return nil, fmt.Errorf("extension is %T; want a EnumSchema", ext)
}
return opts, nil
}
Expand Down Expand Up @@ -2990,8 +2990,8 @@ func getMessageOpenAPIOption(reg *descriptor.Registry, msg *descriptor.Message)
return opts, nil
}

func getEnumOpenAPIOption(reg *descriptor.Registry, enum *descriptor.Enum) (*openapi_options.Schema, error) {
opts, err := extractSchemaOptionFromEnumDescriptor(enum.EnumDescriptorProto)
func getEnumOpenAPIOption(reg *descriptor.Registry, enum *descriptor.Enum) (*openapi_options.EnumSchema, error) {
opts, err := extractEnumSchemaOptionFromEnumDescriptor(enum.EnumDescriptorProto)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -3169,6 +3169,24 @@ func updateSwaggerObjectFromFieldBehavior(s *openapiSchemaObject, j []annotation
}
}

func openapiSchemaFromProtoEnumSchema(s *openapi_options.EnumSchema, reg *descriptor.Registry, refs refMap, data interface{}) openapiSchemaObject {
ret := openapiSchemaObject{
ExternalDocs: protoExternalDocumentationToOpenAPIExternalDocumentation(s.GetExternalDocs(), reg, data),
}
jsonSchema := &openapi_options.JSONSchema{
Ref: s.Ref,
Title: s.Title,
Extensions: s.Extensions,
Description: s.Description,
Default: s.Default,
ReadOnly: s.ReadOnly,
Example: s.Example,
}
ret.schemaCore = protoJSONSchemaToOpenAPISchemaCore(jsonSchema, reg, refs)
updateswaggerObjectFromJSONSchema(&ret, jsonSchema, reg, data)
return ret
}

func openapiSchemaFromProtoSchema(s *openapi_options.Schema, reg *descriptor.Registry, refs refMap, data interface{}) openapiSchemaObject {
ret := openapiSchemaObject{
ExternalDocs: protoExternalDocumentationToOpenAPIExternalDocumentation(s.GetExternalDocs(), reg, data),
Expand Down
65 changes: 33 additions & 32 deletions protoc-gen-openapiv2/options/annotations.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-openapiv2/options/annotations.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extend google.protobuf.EnumOptions {
//
// All IDs are the same, as assigned. It is okay that they are the same, as they extend
// different descriptor messages.
Schema openapiv2_enum = 1042;
EnumSchema openapiv2_enum = 1042;
}
extend google.protobuf.ServiceOptions {
// ID assigned by [email protected] for gRPC-Gateway project.
Expand Down
Loading

0 comments on commit bb30cb9

Please sign in to comment.