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

fix: fetch field property option for path variable and query (#436) #438

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -75,6 +75,13 @@ service Messaging1 {
]
};
}
// while field in message has overriden field, it should reflect in "parameters" section
rpc GetMessageById(Message2) returns(Message2) {
option(google.api.http) = {
// id should have limit since id in message2 have the limit
get: "/v1/messages2/{id}"
};
}
}

service Messaging2 {
Expand All @@ -94,3 +101,16 @@ message Message {
}
];
}

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

string id = 1 [
(openapi.v3.property) = {
title: "this is an overriden field schema title";
max_length: 255;
}
];
}
Expand Up @@ -46,6 +46,33 @@ paths:
$ref: '#/components/schemas/Status'
security:
- BasicAuth: []
/v1/messages2/{id}:
get:
tags:
- Messaging1
description: while field in message has overriden field, it should reflect in "parameters" section
operationId: Messaging1_GetMessageById
parameters:
- name: id
in: path
required: true
schema:
title: this is an overriden field schema title
maxLength: 255
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Message2'
default:
description: Default error response
content:
application/json:
schema:
$ref: '#/components/schemas/Status'
components:
schemas:
GoogleProtobufAny:
Expand All @@ -66,6 +93,14 @@ components:
title: this is an overriden field schema title
maxLength: 255
type: string
Message2:
title: This is an overridden message schema title
type: object
properties:
id:
title: this is an overriden field schema title
maxLength: 255
type: string
Status:
type: object
properties:
Expand Down
Expand Up @@ -46,6 +46,33 @@ paths:
$ref: '#/components/schemas/Status'
security:
- BasicAuth: []
/v1/messages2/{id}:
get:
tags:
- Messaging1
description: while field in message has overriden field, it should reflect in "parameters" section
operationId: Messaging1_GetMessageById
parameters:
- name: id
in: path
required: true
schema:
title: this is an overriden field schema title
maxLength: 255
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Message2'
default:
description: Default error response
content:
application/json:
schema:
$ref: '#/components/schemas/Status'
components:
schemas:
GoogleProtobufAny:
Expand All @@ -66,6 +93,14 @@ components:
title: this is an overriden field schema title
maxLength: 255
type: string
Message2:
title: This is an overridden message schema title
type: object
properties:
id:
title: this is an overriden field schema title
maxLength: 255
type: string
Status:
type: object
properties:
Expand Down
Expand Up @@ -46,6 +46,33 @@ paths:
$ref: '#/components/schemas/google.rpc.Status'
security:
- BasicAuth: []
/v1/messages2/{id}:
get:
tags:
- Messaging1
description: while field in message has overriden field, it should reflect in "parameters" section
operationId: Messaging1_GetMessageById
parameters:
- name: id
in: path
required: true
schema:
title: this is an overriden field schema title
maxLength: 255
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/tests.openapiv3annotations.message.v1.Message2'
default:
description: Default error response
content:
application/json:
schema:
$ref: '#/components/schemas/google.rpc.Status'
components:
schemas:
google.protobuf.Any:
Expand Down Expand Up @@ -82,6 +109,14 @@ components:
title: this is an overriden field schema title
maxLength: 255
type: string
tests.openapiv3annotations.message.v1.Message2:
title: This is an overridden message schema title
type: object
properties:
id:
title: this is an overriden field schema title
maxLength: 255
type: string
securitySchemes:
BasicAuth:
type: http
Expand Down
Expand Up @@ -46,6 +46,33 @@ paths:
$ref: '#/components/schemas/Status'
security:
- BasicAuth: []
/v1/messages2/{id}:
get:
tags:
- Messaging1
description: while field in message has overriden field, it should reflect in "parameters" section
operationId: Messaging1_GetMessageById
parameters:
- name: id
in: path
required: true
schema:
title: this is an overriden field schema title
maxLength: 255
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Message2'
default:
description: Default error response
content:
application/json:
schema:
$ref: '#/components/schemas/Status'
components:
schemas:
GoogleProtobufAny:
Expand All @@ -66,6 +93,14 @@ components:
title: this is an overriden field schema title
maxLength: 255
type: string
Message2:
title: This is an overridden message schema title
type: object
properties:
id:
title: this is an overriden field schema title
maxLength: 255
type: string
Status:
type: object
properties:
Expand Down
Expand Up @@ -46,6 +46,33 @@ paths:
$ref: '#/components/schemas/Status'
security:
- BasicAuth: []
/v1/messages2/{id}:
get:
tags:
- Messaging1
description: while field in message has overriden field, it should reflect in "parameters" section
operationId: Messaging1_GetMessageById
parameters:
- name: id
in: path
required: true
schema:
title: this is an overriden field schema title
maxLength: 255
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Message2'
default:
description: Default error response
content:
application/json:
schema:
$ref: '#/components/schemas/Status'
components:
schemas:
GoogleProtobufAny:
Expand All @@ -66,6 +93,14 @@ components:
title: this is an overriden field schema title
maxLength: 255
type: string
Message2:
title: This is an overridden message schema title
type: object
properties:
id:
title: this is an overriden field schema title
maxLength: 255
type: string
Status:
type: object
properties:
Expand Down
11 changes: 10 additions & 1 deletion cmd/protoc-gen-openapi/generator/generator.go
Expand Up @@ -415,7 +415,7 @@ func (g *OpenAPIv3Generator) _buildQueryParamsV3(field *protogen.Field, depths m
} else if field.Desc.Kind() != protoreflect.GroupKind {
// schemaOrReferenceForField also handles array types
fieldSchema := g.reflect.schemaOrReferenceForField(field.Desc)

enrichFieldSchema(field, fieldSchema)
parameters = append(parameters,
&v3.ParameterOrReference{
Oneof: &v3.ParameterOrReference_Parameter{
Expand All @@ -433,6 +433,14 @@ func (g *OpenAPIv3Generator) _buildQueryParamsV3(field *protogen.Field, depths m
return parameters
}

func enrichFieldSchema(field *protogen.Field, fieldSchema *v3.SchemaOrReference) {
// Merge any `Property` annotations with the current
extProperty := proto.GetExtension(field.Desc.Options(), v3.E_Property)
if extProperty != nil {
proto.Merge(fieldSchema.GetSchema(), extProperty.(*v3.Schema))
}
}

// buildOperationV3 constructs an operation for a set of values.
func (g *OpenAPIv3Generator) buildOperationV3(
d *v3.Document,
Expand Down Expand Up @@ -468,6 +476,7 @@ func (g *OpenAPIv3Generator) buildOperationV3(
field := g.findField(pathParameter, inputMessage)
if field != nil {
fieldSchema = g.reflect.schemaOrReferenceForField(field.Desc)
enrichFieldSchema(field, fieldSchema)
fieldDescription = g.filterCommentString(field.Comments.Leading)
} else {
// If field does not exist, it is safe to set it to string, as it is ignored downstream
Expand Down