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 Message and Field Options #334

Merged
merged 1 commit into from
Apr 5, 2022
Merged
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 @@ -82,6 +82,15 @@ service Messaging2 {
}

message Message {
option (openapi.v3.schema) = {
title: "This is an overridden message schema title";
};

int64 id = 1;
string label = 2;
string label = 2 [
(openapi.v3.property) = {
title: "this is an overriden field schema title";
max_length: 255;
}
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ components:
additionalProperties: true
description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message.
Message:
title: This is an overridden message schema title
type: object
properties:
id:
type: integer
format: int64
label:
title: this is an overriden field schema title
maxLength: 255
type: string
Status:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ components:
additionalProperties: true
description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message.
Message:
title: This is an overridden message schema title
type: object
properties:
id:
type: integer
format: int64
label:
title: this is an overriden field schema title
maxLength: 255
type: string
Status:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ components:
description: A list of messages that carry the error details. There is a common set of message types for APIs to use.
description: 'The `Status` type defines a logical error model that is suitable for different programming environments, including REST APIs and RPC APIs. It is used by [gRPC](https://github.com/grpc). Each `Status` message contains three pieces of data: error code, error message, and error details. You can find out more about this error model and how to work with it in the [API Design Guide](https://cloud.google.com/apis/design/errors).'
tests.openapiv3annotations.message.v1.Message:
title: This is an overridden message schema title
type: object
properties:
id:
type: integer
format: int64
label:
title: this is an overriden field schema title
maxLength: 255
type: string
securitySchemes:
BasicAuth:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ components:
additionalProperties: true
description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message.
Message:
title: This is an overridden message schema title
type: object
properties:
id:
type: integer
format: int64
label:
title: this is an overriden field schema title
maxLength: 255
type: string
Status:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ components:
additionalProperties: true
description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message.
Message:
title: This is an overridden message schema title
type: object
properties:
id:
type: integer
format: int64
label:
title: this is an overriden field schema title
maxLength: 255
type: string
Status:
type: object
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 @@ -795,6 +795,12 @@ func (g *OpenAPIv3Generator) addSchemasForMessagesToDocumentV3(d *v3.Document, m
if inputOnly {
schema.Schema.WriteOnly = true
}

// Merge any `Property` annotations with the current
extProperty := proto.GetExtension(field.Desc.Options(), v3.E_Property)
if extProperty != nil {
proto.Merge(schema.Schema, extProperty.(*v3.Schema))
}
}

definitionProperties.AdditionalProperties = append(
Expand All @@ -806,17 +812,25 @@ func (g *OpenAPIv3Generator) addSchemasForMessagesToDocumentV3(d *v3.Document, m
)
}

schema := &v3.Schema{
Type: "object",
Description: messageDescription,
Properties: definitionProperties,
Required: required,
}

// Merge any `Schema` annotations with the current
extSchema := proto.GetExtension(message.Desc.Options(), v3.E_Schema)
if extSchema != nil {
proto.Merge(schema, extSchema.(*v3.Schema))
}

// Add the schema to the components.schema list.
g.addSchemaToDocumentV3(d, &v3.NamedSchemaOrReference{
Name: schemaName,
Value: &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{
Type: "object",
Description: messageDescription,
Properties: definitionProperties,
Required: required,
},
Schema: schema,
},
},
})
Expand Down
79 changes: 62 additions & 17 deletions openapiv3/annotations.pb.go

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

8 changes: 8 additions & 0 deletions openapiv3/annotations.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ extend google.protobuf.FileOptions {

extend google.protobuf.MethodOptions {
Operation operation = 1042;
}

extend google.protobuf.MessageOptions {
Schema schema = 1042;
}

extend google.protobuf.FieldOptions {
Schema property = 1042;
}