From da894885c40dedc018e4b6dec865858c26cfb4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jagoba=20Gasc=C3=B3n?= Date: Fri, 7 Jun 2024 10:27:59 +0200 Subject: [PATCH] protoc-gen-openapi: remove duplicate body params When a request has both path parameters and body = "*", the path parameters were being repeated in the body. But according to the docs: the special name `*` is used to define that every field not bound by the path template should be mapped to the request body. This commit does exactly that, when the body is `*` and there are some path parameters, then the a new message schema is created that does not include the path parameters. The name of the schema is the same as the message name, with the `_Body` suffix. fixes #323 --- .../google/example/library/v1/openapi.yaml | 24 +- .../library/v1/openapi_default_response.yaml | 24 +- .../library/v1/openapi_fq_schema_naming.yaml | 24 +- .../example/library/v1/openapi_json.yaml | 24 +- .../library/v1/openapi_string_enum.yaml | 24 +- .../openapi_default_response.yaml | 97 +++++++ .../openapi_fq_schema_naming.yaml | 97 +++++++ .../additional_bindings/openapi_json.yaml | 97 +++++++ .../openapi_string_enum.yaml | 97 +++++++ .../examples/tests/allofwrap/openapi.yaml | 26 +- .../allofwrap/openapi_default_response.yaml | 118 +++++++++ .../allofwrap/openapi_fq_schema_naming.yaml | 118 +++++++++ .../tests/allofwrap/openapi_json.yaml | 118 +++++++++ .../tests/allofwrap/openapi_string_enum.yaml | 118 +++++++++ .../examples/tests/mapfields/openapi.yaml | 34 ++- .../mapfields/openapi_default_response.yaml | 34 ++- .../mapfields/openapi_fq_schema_naming.yaml | 34 ++- .../tests/mapfields/openapi_json.yaml | 34 ++- .../tests/mapfields/openapi_string_enum.yaml | 34 ++- .../examples/tests/noannotations/openapi.yaml | 10 +- .../openapi_default_response.yaml | 10 +- .../openapi_fq_schema_naming.yaml | 10 +- .../tests/noannotations/openapi_json.yaml | 10 +- .../noannotations/openapi_string_enum.yaml | 10 +- .../tests/openapiv3annotations/openapi.yaml | 13 +- .../openapi_default_response.yaml | 13 +- .../openapi_fq_schema_naming.yaml | 13 +- .../openapiv3annotations/openapi_json.yaml | 13 +- .../openapi_string_enum.yaml | 13 +- .../examples/tests/pathparams/openapi.yaml | 12 +- .../pathparams/openapi_default_response.yaml | 12 +- .../pathparams/openapi_fq_schema_naming.yaml | 12 +- .../tests/pathparams/openapi_json.yaml | 12 +- .../tests/pathparams/openapi_string_enum.yaml | 12 +- .../examples/tests/protobuftypes/openapi.yaml | 74 +++++- .../openapi_default_response.yaml | 74 +++++- .../openapi_fq_schema_naming.yaml | 74 +++++- .../tests/protobuftypes/openapi_json.yaml | 74 +++++- .../protobuftypes/openapi_string_enum.yaml | 74 +++++- cmd/protoc-gen-openapi/generator/generator.go | 242 ++++++++++-------- metrics/complexity.pb.go | 4 +- metrics/vocabulary.pb.go | 4 +- plugins/plugin.pb.go | 4 +- surface/surface.pb.go | 4 +- 44 files changed, 1754 insertions(+), 225 deletions(-) create mode 100644 cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_default_response.yaml create mode 100644 cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_fq_schema_naming.yaml create mode 100644 cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_json.yaml create mode 100644 cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_string_enum.yaml create mode 100644 cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_default_response.yaml create mode 100644 cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_fq_schema_naming.yaml create mode 100644 cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_json.yaml create mode 100644 cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_string_enum.yaml diff --git a/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi.yaml b/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi.yaml index 219a0b24..90a1383f 100644 --- a/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi.yaml +++ b/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi.yaml @@ -335,7 +335,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/MoveBookRequest' + $ref: '#/components/schemas/MoveBookRequest_Body' required: true responses: "200": @@ -374,7 +374,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/MergeShelvesRequest' + $ref: '#/components/schemas/MergeShelvesRequest_Body' required: true responses: "200": @@ -469,36 +469,24 @@ components: field in the subsequent call to `ListShelves` method to retrieve the next page of results. description: Response message for LibraryService.ListShelves. - MergeShelvesRequest: + MergeShelvesRequest_Body: required: - - name - other_shelf_name type: object properties: - name: - type: string - description: The name of the shelf we're adding books to. other_shelf_name: type: string description: The name of the shelf we're removing books from and deleting. - description: |- - Describes the shelf being removed (other_shelf_name) and updated - (name) in this merge. - MoveBookRequest: + description: The body of LibraryService_MergeShelves + MoveBookRequest_Body: required: - - name - other_shelf_name type: object properties: - name: - type: string - description: The name of the book to move. other_shelf_name: type: string description: The name of the destination shelf. - description: |- - Describes what book to move (name) and what shelf we're moving it - to (other_shelf_name). + description: The body of LibraryService_MoveBook Shelf: required: - name diff --git a/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_default_response.yaml b/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_default_response.yaml index f1ecadb6..e4d3f621 100644 --- a/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_default_response.yaml +++ b/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_default_response.yaml @@ -335,7 +335,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/MoveBookRequest' + $ref: '#/components/schemas/MoveBookRequest_Body' required: true responses: "200": @@ -374,7 +374,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/MergeShelvesRequest' + $ref: '#/components/schemas/MergeShelvesRequest_Body' required: true responses: "200": @@ -469,36 +469,24 @@ components: field in the subsequent call to `ListShelves` method to retrieve the next page of results. description: Response message for LibraryService.ListShelves. - MergeShelvesRequest: + MergeShelvesRequest_Body: required: - - name - otherShelfName type: object properties: - name: - type: string - description: The name of the shelf we're adding books to. otherShelfName: type: string description: The name of the shelf we're removing books from and deleting. - description: |- - Describes the shelf being removed (other_shelf_name) and updated - (name) in this merge. - MoveBookRequest: + description: The body of LibraryService_MergeShelves + MoveBookRequest_Body: required: - - name - otherShelfName type: object properties: - name: - type: string - description: The name of the book to move. otherShelfName: type: string description: The name of the destination shelf. - description: |- - Describes what book to move (name) and what shelf we're moving it - to (other_shelf_name). + description: The body of LibraryService_MoveBook Shelf: required: - name diff --git a/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_fq_schema_naming.yaml b/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_fq_schema_naming.yaml index 2b908fa3..fefa4a48 100644 --- a/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_fq_schema_naming.yaml +++ b/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_fq_schema_naming.yaml @@ -335,7 +335,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/google.example.library.v1.MoveBookRequest' + $ref: '#/components/schemas/google.example.library.v1.MoveBookRequest_Body' required: true responses: "200": @@ -374,7 +374,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/google.example.library.v1.MergeShelvesRequest' + $ref: '#/components/schemas/google.example.library.v1.MergeShelvesRequest_Body' required: true responses: "200": @@ -461,36 +461,24 @@ components: field in the subsequent call to `ListShelves` method to retrieve the next page of results. description: Response message for LibraryService.ListShelves. - google.example.library.v1.MergeShelvesRequest: + google.example.library.v1.MergeShelvesRequest_Body: required: - - name - otherShelfName type: object properties: - name: - type: string - description: The name of the shelf we're adding books to. otherShelfName: type: string description: The name of the shelf we're removing books from and deleting. - description: |- - Describes the shelf being removed (other_shelf_name) and updated - (name) in this merge. - google.example.library.v1.MoveBookRequest: + description: The body of LibraryService_MergeShelves + google.example.library.v1.MoveBookRequest_Body: required: - - name - otherShelfName type: object properties: - name: - type: string - description: The name of the book to move. otherShelfName: type: string description: The name of the destination shelf. - description: |- - Describes what book to move (name) and what shelf we're moving it - to (other_shelf_name). + description: The body of LibraryService_MoveBook google.example.library.v1.Shelf: required: - name diff --git a/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_json.yaml b/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_json.yaml index a0ea21f4..f31afd11 100644 --- a/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_json.yaml +++ b/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_json.yaml @@ -335,7 +335,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/MoveBookRequest' + $ref: '#/components/schemas/MoveBookRequest_Body' required: true responses: "200": @@ -374,7 +374,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/MergeShelvesRequest' + $ref: '#/components/schemas/MergeShelvesRequest_Body' required: true responses: "200": @@ -469,36 +469,24 @@ components: field in the subsequent call to `ListShelves` method to retrieve the next page of results. description: Response message for LibraryService.ListShelves. - MergeShelvesRequest: + MergeShelvesRequest_Body: required: - - name - otherShelfName type: object properties: - name: - type: string - description: The name of the shelf we're adding books to. otherShelfName: type: string description: The name of the shelf we're removing books from and deleting. - description: |- - Describes the shelf being removed (other_shelf_name) and updated - (name) in this merge. - MoveBookRequest: + description: The body of LibraryService_MergeShelves + MoveBookRequest_Body: required: - - name - otherShelfName type: object properties: - name: - type: string - description: The name of the book to move. otherShelfName: type: string description: The name of the destination shelf. - description: |- - Describes what book to move (name) and what shelf we're moving it - to (other_shelf_name). + description: The body of LibraryService_MoveBook Shelf: required: - name diff --git a/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_string_enum.yaml b/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_string_enum.yaml index f1ecadb6..e4d3f621 100644 --- a/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_string_enum.yaml +++ b/cmd/protoc-gen-openapi/examples/google/example/library/v1/openapi_string_enum.yaml @@ -335,7 +335,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/MoveBookRequest' + $ref: '#/components/schemas/MoveBookRequest_Body' required: true responses: "200": @@ -374,7 +374,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/MergeShelvesRequest' + $ref: '#/components/schemas/MergeShelvesRequest_Body' required: true responses: "200": @@ -469,36 +469,24 @@ components: field in the subsequent call to `ListShelves` method to retrieve the next page of results. description: Response message for LibraryService.ListShelves. - MergeShelvesRequest: + MergeShelvesRequest_Body: required: - - name - otherShelfName type: object properties: - name: - type: string - description: The name of the shelf we're adding books to. otherShelfName: type: string description: The name of the shelf we're removing books from and deleting. - description: |- - Describes the shelf being removed (other_shelf_name) and updated - (name) in this merge. - MoveBookRequest: + description: The body of LibraryService_MergeShelves + MoveBookRequest_Body: required: - - name - otherShelfName type: object properties: - name: - type: string - description: The name of the book to move. otherShelfName: type: string description: The name of the destination shelf. - description: |- - Describes what book to move (name) and what shelf we're moving it - to (other_shelf_name). + description: The body of LibraryService_MoveBook Shelf: required: - name diff --git a/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_default_response.yaml b/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_default_response.yaml new file mode 100644 index 00000000..f465262f --- /dev/null +++ b/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_default_response.yaml @@ -0,0 +1,97 @@ +# Generated with protoc-gen-openapi +# https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi + +openapi: 3.0.3 +info: + title: Messaging API + version: 0.0.1 +paths: + /v1/messages: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/Status' + /v1/messages/{messageId}: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + parameters: + - name: messageId + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + type: string + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/Status' +components: + schemas: + GoogleProtobufAny: + type: object + properties: + '@type': + type: string + description: The type of the serialized message. + additionalProperties: true + description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message. + Message: + type: object + properties: + messageId: + type: string + text: + type: string + Status: + type: object + properties: + code: + type: integer + description: The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. + format: int32 + message: + type: string + description: A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. + details: + type: array + items: + $ref: '#/components/schemas/GoogleProtobufAny' + 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).' +tags: + - name: Messaging diff --git a/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_fq_schema_naming.yaml b/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_fq_schema_naming.yaml new file mode 100644 index 00000000..f3762491 --- /dev/null +++ b/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_fq_schema_naming.yaml @@ -0,0 +1,97 @@ +# Generated with protoc-gen-openapi +# https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi + +openapi: 3.0.3 +info: + title: Messaging API + version: 0.0.1 +paths: + /v1/messages: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/tests.additional_bindings.message.v1.Message' + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/tests.additional_bindings.message.v1.Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/google.rpc.Status' + /v1/messages/{messageId}: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + parameters: + - name: messageId + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + type: string + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/tests.additional_bindings.message.v1.Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/google.rpc.Status' +components: + schemas: + google.protobuf.Any: + type: object + properties: + '@type': + type: string + description: The type of the serialized message. + additionalProperties: true + description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message. + google.rpc.Status: + type: object + properties: + code: + type: integer + description: The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. + format: int32 + message: + type: string + description: A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. + details: + type: array + items: + $ref: '#/components/schemas/google.protobuf.Any' + 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.additional_bindings.message.v1.Message: + type: object + properties: + messageId: + type: string + text: + type: string +tags: + - name: Messaging diff --git a/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_json.yaml b/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_json.yaml new file mode 100644 index 00000000..9f40f3bf --- /dev/null +++ b/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_json.yaml @@ -0,0 +1,97 @@ +# Generated with protoc-gen-openapi +# https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi + +openapi: 3.0.3 +info: + title: Messaging API + version: 1.2.3 +paths: + /v1/messages: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/Status' + /v1/messages/{messageId}: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + parameters: + - name: messageId + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + type: string + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/Status' +components: + schemas: + GoogleProtobufAny: + type: object + properties: + '@type': + type: string + description: The type of the serialized message. + additionalProperties: true + description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message. + Message: + type: object + properties: + messageId: + type: string + text: + type: string + Status: + type: object + properties: + code: + type: integer + description: The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. + format: int32 + message: + type: string + description: A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. + details: + type: array + items: + $ref: '#/components/schemas/GoogleProtobufAny' + 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).' +tags: + - name: Messaging diff --git a/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_string_enum.yaml b/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_string_enum.yaml new file mode 100644 index 00000000..f465262f --- /dev/null +++ b/cmd/protoc-gen-openapi/examples/tests/additional_bindings/openapi_string_enum.yaml @@ -0,0 +1,97 @@ +# Generated with protoc-gen-openapi +# https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi + +openapi: 3.0.3 +info: + title: Messaging API + version: 0.0.1 +paths: + /v1/messages: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/Status' + /v1/messages/{messageId}: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + parameters: + - name: messageId + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + type: string + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/Status' +components: + schemas: + GoogleProtobufAny: + type: object + properties: + '@type': + type: string + description: The type of the serialized message. + additionalProperties: true + description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message. + Message: + type: object + properties: + messageId: + type: string + text: + type: string + Status: + type: object + properties: + code: + type: integer + description: The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. + format: int32 + message: + type: string + description: A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. + details: + type: array + items: + $ref: '#/components/schemas/GoogleProtobufAny' + 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).' +tags: + - name: Messaging diff --git a/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi.yaml b/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi.yaml index c9c35391..f1b9773c 100644 --- a/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -69,6 +69,30 @@ components: items: $ref: '#/components/schemas/Message_Sub' description: test repeated, should not allof wrapped + Message_Body: + type: object + properties: + sub: + $ref: '#/components/schemas/Message_Sub' + sub_input: + writeOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + sub_output: + readOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + sub_desc: + allOf: + - $ref: '#/components/schemas/Message_Sub' + description: this sub has a description + subs: + readOnly: true + type: array + items: + $ref: '#/components/schemas/Message_Sub' + description: test repeated, should not allof wrapped + description: The body of Messaging_UpdateMessage Message_Sub: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_default_response.yaml b/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_default_response.yaml new file mode 100644 index 00000000..072044e2 --- /dev/null +++ b/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_default_response.yaml @@ -0,0 +1,118 @@ +# Generated with protoc-gen-openapi +# https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi + +openapi: 3.0.3 +info: + title: Messaging API + version: 0.0.1 +paths: + /v1/messages/{message_id}: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + parameters: + - name: message_id + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Message_Body' + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/Status' +components: + schemas: + GoogleProtobufAny: + type: object + properties: + '@type': + type: string + description: The type of the serialized message. + additionalProperties: true + description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message. + Message: + type: object + properties: + sub: + $ref: '#/components/schemas/Message_Sub' + subInput: + writeOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subOutput: + readOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subDesc: + allOf: + - $ref: '#/components/schemas/Message_Sub' + description: this sub has a description + subs: + readOnly: true + type: array + items: + $ref: '#/components/schemas/Message_Sub' + description: test repeated, should not allof wrapped + Message_Body: + type: object + properties: + sub: + $ref: '#/components/schemas/Message_Sub' + subInput: + writeOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subOutput: + readOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subDesc: + allOf: + - $ref: '#/components/schemas/Message_Sub' + description: this sub has a description + subs: + readOnly: true + type: array + items: + $ref: '#/components/schemas/Message_Sub' + description: test repeated, should not allof wrapped + description: The body of Messaging_UpdateMessage + Message_Sub: + type: object + properties: + content: + type: string + Status: + type: object + properties: + code: + type: integer + description: The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. + format: int32 + message: + type: string + description: A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. + details: + type: array + items: + $ref: '#/components/schemas/GoogleProtobufAny' + 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).' +tags: + - name: Messaging diff --git a/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_fq_schema_naming.yaml b/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_fq_schema_naming.yaml new file mode 100644 index 00000000..263b949a --- /dev/null +++ b/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_fq_schema_naming.yaml @@ -0,0 +1,118 @@ +# Generated with protoc-gen-openapi +# https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi + +openapi: 3.0.3 +info: + title: Messaging API + version: 0.0.1 +paths: + /v1/messages/{message_id}: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + parameters: + - name: message_id + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Body' + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/tests.allofwrap.message.v1.Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/google.rpc.Status' +components: + schemas: + google.protobuf.Any: + type: object + properties: + '@type': + type: string + description: The type of the serialized message. + additionalProperties: true + description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message. + google.rpc.Status: + type: object + properties: + code: + type: integer + description: The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. + format: int32 + message: + type: string + description: A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. + details: + type: array + items: + $ref: '#/components/schemas/google.protobuf.Any' + 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.allofwrap.message.v1.Message: + type: object + properties: + sub: + $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Sub' + subInput: + writeOnly: true + allOf: + - $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Sub' + subOutput: + readOnly: true + allOf: + - $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Sub' + subDesc: + allOf: + - $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Sub' + description: this sub has a description + subs: + readOnly: true + type: array + items: + $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Sub' + description: test repeated, should not allof wrapped + tests.allofwrap.message.v1.Message_Body: + type: object + properties: + sub: + $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Sub' + subInput: + writeOnly: true + allOf: + - $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Sub' + subOutput: + readOnly: true + allOf: + - $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Sub' + subDesc: + allOf: + - $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Sub' + description: this sub has a description + subs: + readOnly: true + type: array + items: + $ref: '#/components/schemas/tests.allofwrap.message.v1.Message_Sub' + description: test repeated, should not allof wrapped + description: The body of Messaging_UpdateMessage + tests.allofwrap.message.v1.Message_Sub: + type: object + properties: + content: + type: string +tags: + - name: Messaging diff --git a/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_json.yaml b/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_json.yaml new file mode 100644 index 00000000..54a2e301 --- /dev/null +++ b/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_json.yaml @@ -0,0 +1,118 @@ +# Generated with protoc-gen-openapi +# https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi + +openapi: 3.0.3 +info: + title: Messaging API + version: 1.2.3 +paths: + /v1/messages/{message_id}: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + parameters: + - name: message_id + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Message_Body' + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/Status' +components: + schemas: + GoogleProtobufAny: + type: object + properties: + '@type': + type: string + description: The type of the serialized message. + additionalProperties: true + description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message. + Message: + type: object + properties: + sub: + $ref: '#/components/schemas/Message_Sub' + subInput: + writeOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subOutput: + readOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subDesc: + allOf: + - $ref: '#/components/schemas/Message_Sub' + description: this sub has a description + subs: + readOnly: true + type: array + items: + $ref: '#/components/schemas/Message_Sub' + description: test repeated, should not allof wrapped + Message_Body: + type: object + properties: + sub: + $ref: '#/components/schemas/Message_Sub' + subInput: + writeOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subOutput: + readOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subDesc: + allOf: + - $ref: '#/components/schemas/Message_Sub' + description: this sub has a description + subs: + readOnly: true + type: array + items: + $ref: '#/components/schemas/Message_Sub' + description: test repeated, should not allof wrapped + description: The body of Messaging_UpdateMessage + Message_Sub: + type: object + properties: + content: + type: string + Status: + type: object + properties: + code: + type: integer + description: The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. + format: int32 + message: + type: string + description: A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. + details: + type: array + items: + $ref: '#/components/schemas/GoogleProtobufAny' + 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).' +tags: + - name: Messaging diff --git a/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_string_enum.yaml b/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_string_enum.yaml new file mode 100644 index 00000000..072044e2 --- /dev/null +++ b/cmd/protoc-gen-openapi/examples/tests/allofwrap/openapi_string_enum.yaml @@ -0,0 +1,118 @@ +# Generated with protoc-gen-openapi +# https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi + +openapi: 3.0.3 +info: + title: Messaging API + version: 0.0.1 +paths: + /v1/messages/{message_id}: + patch: + tags: + - Messaging + operationId: Messaging_UpdateMessage + parameters: + - name: message_id + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Message_Body' + required: true + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Message' + default: + description: Default error response + content: + application/json: + schema: + $ref: '#/components/schemas/Status' +components: + schemas: + GoogleProtobufAny: + type: object + properties: + '@type': + type: string + description: The type of the serialized message. + additionalProperties: true + description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message. + Message: + type: object + properties: + sub: + $ref: '#/components/schemas/Message_Sub' + subInput: + writeOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subOutput: + readOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subDesc: + allOf: + - $ref: '#/components/schemas/Message_Sub' + description: this sub has a description + subs: + readOnly: true + type: array + items: + $ref: '#/components/schemas/Message_Sub' + description: test repeated, should not allof wrapped + Message_Body: + type: object + properties: + sub: + $ref: '#/components/schemas/Message_Sub' + subInput: + writeOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subOutput: + readOnly: true + allOf: + - $ref: '#/components/schemas/Message_Sub' + subDesc: + allOf: + - $ref: '#/components/schemas/Message_Sub' + description: this sub has a description + subs: + readOnly: true + type: array + items: + $ref: '#/components/schemas/Message_Sub' + description: test repeated, should not allof wrapped + description: The body of Messaging_UpdateMessage + Message_Sub: + type: object + properties: + content: + type: string + Status: + type: object + properties: + code: + type: integer + description: The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code]. + format: int32 + message: + type: string + description: A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client. + details: + type: array + items: + $ref: '#/components/schemas/GoogleProtobufAny' + 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).' +tags: + - name: Messaging diff --git a/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi.yaml b/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi.yaml index c63dc62e..6595e4de 100644 --- a/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -86,6 +86,38 @@ components: type: object additionalProperties: type: object + Message_Body: + type: object + properties: + another_message: + $ref: '#/components/schemas/AnotherMessage' + sub_message: + $ref: '#/components/schemas/Message_SubMessage' + string_list: + type: array + items: + type: string + sub_message_list: + type: array + items: + $ref: '#/components/schemas/Message_SubMessage' + object_list: + type: array + items: + type: object + strings_map: + type: object + additionalProperties: + type: string + sub_messages_map: + type: object + additionalProperties: + $ref: '#/components/schemas/Message_SubMessage' + objects_map: + type: object + additionalProperties: + type: object + description: The body of Messaging_UpdateMessage Message_SubMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_default_response.yaml b/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_default_response.yaml index 4d510192..4f003a83 100644 --- a/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_default_response.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_default_response.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -86,6 +86,38 @@ components: type: object additionalProperties: type: object + Message_Body: + type: object + properties: + anotherMessage: + $ref: '#/components/schemas/AnotherMessage' + subMessage: + $ref: '#/components/schemas/Message_SubMessage' + stringList: + type: array + items: + type: string + subMessageList: + type: array + items: + $ref: '#/components/schemas/Message_SubMessage' + objectList: + type: array + items: + type: object + stringsMap: + type: object + additionalProperties: + type: string + subMessagesMap: + type: object + additionalProperties: + $ref: '#/components/schemas/Message_SubMessage' + objectsMap: + type: object + additionalProperties: + type: object + description: The body of Messaging_UpdateMessage Message_SubMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_fq_schema_naming.yaml b/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_fq_schema_naming.yaml index 1c343d1e..5e65d52f 100644 --- a/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_fq_schema_naming.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_fq_schema_naming.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/tests.mapfields.message.v1.Message' + $ref: '#/components/schemas/tests.mapfields.message.v1.Message_Body' required: true responses: "200": @@ -102,6 +102,38 @@ components: type: object additionalProperties: type: object + tests.mapfields.message.v1.Message_Body: + type: object + properties: + anotherMessage: + $ref: '#/components/schemas/tests.mapfields.message.v1.AnotherMessage' + subMessage: + $ref: '#/components/schemas/tests.mapfields.message.v1.Message_SubMessage' + stringList: + type: array + items: + type: string + subMessageList: + type: array + items: + $ref: '#/components/schemas/tests.mapfields.message.v1.Message_SubMessage' + objectList: + type: array + items: + type: object + stringsMap: + type: object + additionalProperties: + type: string + subMessagesMap: + type: object + additionalProperties: + $ref: '#/components/schemas/tests.mapfields.message.v1.Message_SubMessage' + objectsMap: + type: object + additionalProperties: + type: object + description: The body of Messaging_UpdateMessage tests.mapfields.message.v1.Message_SubMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_json.yaml b/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_json.yaml index 8e8b796e..aa07cfec 100644 --- a/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_json.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_json.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -86,6 +86,38 @@ components: type: object additionalProperties: type: object + Message_Body: + type: object + properties: + anotherMessage: + $ref: '#/components/schemas/AnotherMessage' + subMessage: + $ref: '#/components/schemas/Message_SubMessage' + stringList: + type: array + items: + type: string + subMessageList: + type: array + items: + $ref: '#/components/schemas/Message_SubMessage' + objectList: + type: array + items: + type: object + stringsMap: + type: object + additionalProperties: + type: string + subMessagesMap: + type: object + additionalProperties: + $ref: '#/components/schemas/Message_SubMessage' + objectsMap: + type: object + additionalProperties: + type: object + description: The body of Messaging_UpdateMessage Message_SubMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_string_enum.yaml b/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_string_enum.yaml index 4d510192..4f003a83 100644 --- a/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_string_enum.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/mapfields/openapi_string_enum.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -86,6 +86,38 @@ components: type: object additionalProperties: type: object + Message_Body: + type: object + properties: + anotherMessage: + $ref: '#/components/schemas/AnotherMessage' + subMessage: + $ref: '#/components/schemas/Message_SubMessage' + stringList: + type: array + items: + type: string + subMessageList: + type: array + items: + $ref: '#/components/schemas/Message_SubMessage' + objectList: + type: array + items: + type: object + stringsMap: + type: object + additionalProperties: + type: string + subMessagesMap: + type: object + additionalProperties: + $ref: '#/components/schemas/Message_SubMessage' + objectsMap: + type: object + additionalProperties: + type: object + description: The body of Messaging_UpdateMessage Message_SubMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi.yaml b/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi.yaml index d00cbb93..87a1bce0 100644 --- a/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -53,6 +53,14 @@ components: type: string label: type: string + Message_Body: + type: object + properties: + id: + type: string + label: + type: string + description: The body of Messaging1_UpdateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_default_response.yaml b/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_default_response.yaml index d00cbb93..87a1bce0 100644 --- a/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_default_response.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_default_response.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -53,6 +53,14 @@ components: type: string label: type: string + Message_Body: + type: object + properties: + id: + type: string + label: + type: string + description: The body of Messaging1_UpdateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_fq_schema_naming.yaml b/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_fq_schema_naming.yaml index 7f2e9e79..acef3054 100644 --- a/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_fq_schema_naming.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_fq_schema_naming.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/tests.noannotations.message.v1.Message' + $ref: '#/components/schemas/tests.noannotations.message.v1.Message_Body' required: true responses: "200": @@ -69,5 +69,13 @@ components: type: string label: type: string + tests.noannotations.message.v1.Message_Body: + type: object + properties: + id: + type: string + label: + type: string + description: The body of Messaging1_UpdateMessage tags: - name: Messaging1 diff --git a/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_json.yaml b/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_json.yaml index d7d35208..ae8260cb 100644 --- a/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_json.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_json.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -53,6 +53,14 @@ components: type: string label: type: string + Message_Body: + type: object + properties: + id: + type: string + label: + type: string + description: The body of Messaging1_UpdateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_string_enum.yaml b/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_string_enum.yaml index d00cbb93..87a1bce0 100644 --- a/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_string_enum.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/noannotations/openapi_string_enum.yaml @@ -21,7 +21,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -53,6 +53,14 @@ components: type: string label: type: string + Message_Body: + type: object + properties: + id: + type: string + label: + type: string + description: The body of Messaging1_UpdateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi.yaml b/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi.yaml index b1e31f0f..68847c41 100644 --- a/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi.yaml @@ -29,7 +29,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -66,6 +66,17 @@ components: title: this is an overriden field schema title maxLength: 255 type: string + Message_Body: + title: This is an overridden message schema title + type: object + properties: + id: + type: string + label: + title: this is an overriden field schema title + maxLength: 255 + type: string + description: The body of Messaging1_UpdateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_default_response.yaml b/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_default_response.yaml index b1e31f0f..68847c41 100644 --- a/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_default_response.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_default_response.yaml @@ -29,7 +29,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -66,6 +66,17 @@ components: title: this is an overriden field schema title maxLength: 255 type: string + Message_Body: + title: This is an overridden message schema title + type: object + properties: + id: + type: string + label: + title: this is an overriden field schema title + maxLength: 255 + type: string + description: The body of Messaging1_UpdateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_fq_schema_naming.yaml b/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_fq_schema_naming.yaml index 5dbbceca..4b8d6c4a 100644 --- a/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_fq_schema_naming.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_fq_schema_naming.yaml @@ -29,7 +29,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/tests.openapiv3annotations.message.v1.Message' + $ref: '#/components/schemas/tests.openapiv3annotations.message.v1.Message_Body' required: true responses: "200": @@ -82,6 +82,17 @@ components: title: this is an overriden field schema title maxLength: 255 type: string + tests.openapiv3annotations.message.v1.Message_Body: + title: This is an overridden message schema title + type: object + properties: + id: + type: string + label: + title: this is an overriden field schema title + maxLength: 255 + type: string + description: The body of Messaging1_UpdateMessage securitySchemes: BasicAuth: type: http diff --git a/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_json.yaml b/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_json.yaml index b1e31f0f..68847c41 100644 --- a/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_json.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_json.yaml @@ -29,7 +29,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -66,6 +66,17 @@ components: title: this is an overriden field schema title maxLength: 255 type: string + Message_Body: + title: This is an overridden message schema title + type: object + properties: + id: + type: string + label: + title: this is an overriden field schema title + maxLength: 255 + type: string + description: The body of Messaging1_UpdateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_string_enum.yaml b/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_string_enum.yaml index b1e31f0f..68847c41 100644 --- a/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_string_enum.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/openapiv3annotations/openapi_string_enum.yaml @@ -29,7 +29,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -66,6 +66,17 @@ components: title: this is an overriden field schema title maxLength: 255 type: string + Message_Body: + title: This is an overridden message schema title + type: object + properties: + id: + type: string + label: + title: this is an overriden field schema title + maxLength: 255 + type: string + description: The body of Messaging1_UpdateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi.yaml b/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi.yaml index f65b66d2..14f352d8 100644 --- a/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi.yaml @@ -48,7 +48,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -113,6 +113,16 @@ components: type: string maybe: type: string + Message_Body: + type: object + properties: + user_id: + type: string + content: + type: string + maybe: + type: string + description: The body of Messaging_CreateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_default_response.yaml b/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_default_response.yaml index 470bab61..8d2dc097 100644 --- a/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_default_response.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_default_response.yaml @@ -48,7 +48,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -113,6 +113,16 @@ components: type: string maybe: type: string + Message_Body: + type: object + properties: + userId: + type: string + content: + type: string + maybe: + type: string + description: The body of Messaging_CreateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_fq_schema_naming.yaml b/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_fq_schema_naming.yaml index f682585d..c5436954 100644 --- a/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_fq_schema_naming.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_fq_schema_naming.yaml @@ -48,7 +48,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/tests.pathparams.message.v1.Message' + $ref: '#/components/schemas/tests.pathparams.message.v1.Message_Body' required: true responses: "200": @@ -129,5 +129,15 @@ components: type: string maybe: type: string + tests.pathparams.message.v1.Message_Body: + type: object + properties: + userId: + type: string + content: + type: string + maybe: + type: string + description: The body of Messaging_CreateMessage tags: - name: Messaging diff --git a/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_json.yaml b/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_json.yaml index ff7d0a3d..d729f905 100644 --- a/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_json.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_json.yaml @@ -48,7 +48,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -113,6 +113,16 @@ components: type: string maybe: type: string + Message_Body: + type: object + properties: + userId: + type: string + content: + type: string + maybe: + type: string + description: The body of Messaging_CreateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_string_enum.yaml b/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_string_enum.yaml index 470bab61..8d2dc097 100644 --- a/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_string_enum.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/pathparams/openapi_string_enum.yaml @@ -48,7 +48,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -113,6 +113,16 @@ components: type: string maybe: type: string + Message_Body: + type: object + properties: + userId: + type: string + content: + type: string + maybe: + type: string + description: The body of Messaging_CreateMessage Status: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi.yaml b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi.yaml index 6cb6b7fb..acbbe956 100644 --- a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi.yaml @@ -177,7 +177,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -459,6 +459,78 @@ components: duration_type: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string + Message_Body: + type: object + properties: + string_type: + type: string + recursive_type: + $ref: '#/components/schemas/RecursiveParent' + embedded_type: + $ref: '#/components/schemas/Message_EmbMessage' + sub_type: + $ref: '#/components/schemas/SubMessage' + repeated_type: + type: array + items: + type: string + repeated_sub_type: + type: array + items: + $ref: '#/components/schemas/SubMessage' + repeated_recursive_type: + type: array + items: + $ref: '#/components/schemas/RecursiveParent' + map_type: + type: object + additionalProperties: + type: string + body: + type: object + media: + type: array + items: + type: object + value_type: + allOf: + - $ref: '#/components/schemas/GoogleProtobufValue' + description: Description of value + repeated_value_type: + type: array + items: + $ref: '#/components/schemas/GoogleProtobufValue' + description: Description of repeated value + bool_value_type: + type: boolean + bytes_value_type: + type: string + format: bytes + int32_value_type: + type: integer + format: int32 + uint32_value_type: + type: integer + format: uint32 + string_value_type: + type: string + int64_value_type: + type: string + uint64_value_type: + type: string + float_value_type: + type: number + format: float + double_value_type: + type: number + format: double + timestamp_type: + type: string + format: date-time + duration_type: + pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ + type: string + description: The body of Messaging_CreateMessage Message_EmbMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_default_response.yaml b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_default_response.yaml index 1158893e..b2703e17 100644 --- a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_default_response.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_default_response.yaml @@ -177,7 +177,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -459,6 +459,78 @@ components: durationType: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string + Message_Body: + type: object + properties: + stringType: + type: string + recursiveType: + $ref: '#/components/schemas/RecursiveParent' + embeddedType: + $ref: '#/components/schemas/Message_EmbMessage' + subType: + $ref: '#/components/schemas/SubMessage' + repeatedType: + type: array + items: + type: string + repeatedSubType: + type: array + items: + $ref: '#/components/schemas/SubMessage' + repeatedRecursiveType: + type: array + items: + $ref: '#/components/schemas/RecursiveParent' + mapType: + type: object + additionalProperties: + type: string + body: + type: object + media: + type: array + items: + type: object + valueType: + allOf: + - $ref: '#/components/schemas/GoogleProtobufValue' + description: Description of value + repeatedValueType: + type: array + items: + $ref: '#/components/schemas/GoogleProtobufValue' + description: Description of repeated value + boolValueType: + type: boolean + bytesValueType: + type: string + format: bytes + int32ValueType: + type: integer + format: int32 + uint32ValueType: + type: integer + format: uint32 + stringValueType: + type: string + int64ValueType: + type: string + uint64ValueType: + type: string + floatValueType: + type: number + format: float + doubleValueType: + type: number + format: double + timestampType: + type: string + format: date-time + durationType: + pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ + type: string + description: The body of Messaging_CreateMessage Message_EmbMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_fq_schema_naming.yaml b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_fq_schema_naming.yaml index bd276315..992cd159 100644 --- a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_fq_schema_naming.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_fq_schema_naming.yaml @@ -177,7 +177,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/tests.protobuftypes.message.v1.Message' + $ref: '#/components/schemas/tests.protobuftypes.message.v1.Message_Body' required: true responses: "200": @@ -475,6 +475,78 @@ components: durationType: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string + tests.protobuftypes.message.v1.Message_Body: + type: object + properties: + stringType: + type: string + recursiveType: + $ref: '#/components/schemas/tests.protobuftypes.message.v1.RecursiveParent' + embeddedType: + $ref: '#/components/schemas/tests.protobuftypes.message.v1.Message_EmbMessage' + subType: + $ref: '#/components/schemas/tests.protobuftypes.message.v1.SubMessage' + repeatedType: + type: array + items: + type: string + repeatedSubType: + type: array + items: + $ref: '#/components/schemas/tests.protobuftypes.message.v1.SubMessage' + repeatedRecursiveType: + type: array + items: + $ref: '#/components/schemas/tests.protobuftypes.message.v1.RecursiveParent' + mapType: + type: object + additionalProperties: + type: string + body: + type: object + media: + type: array + items: + type: object + valueType: + allOf: + - $ref: '#/components/schemas/google.protobuf.Value' + description: Description of value + repeatedValueType: + type: array + items: + $ref: '#/components/schemas/google.protobuf.Value' + description: Description of repeated value + boolValueType: + type: boolean + bytesValueType: + type: string + format: bytes + int32ValueType: + type: integer + format: int32 + uint32ValueType: + type: integer + format: uint32 + stringValueType: + type: string + int64ValueType: + type: string + uint64ValueType: + type: string + floatValueType: + type: number + format: float + doubleValueType: + type: number + format: double + timestampType: + type: string + format: date-time + durationType: + pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ + type: string + description: The body of Messaging_CreateMessage tests.protobuftypes.message.v1.Message_EmbMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_json.yaml b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_json.yaml index ae946c55..1360925f 100644 --- a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_json.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_json.yaml @@ -177,7 +177,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -459,6 +459,78 @@ components: durationType: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string + Message_Body: + type: object + properties: + stringType: + type: string + recursiveType: + $ref: '#/components/schemas/RecursiveParent' + embeddedType: + $ref: '#/components/schemas/Message_EmbMessage' + subType: + $ref: '#/components/schemas/SubMessage' + repeatedType: + type: array + items: + type: string + repeatedSubType: + type: array + items: + $ref: '#/components/schemas/SubMessage' + repeatedRecursiveType: + type: array + items: + $ref: '#/components/schemas/RecursiveParent' + mapType: + type: object + additionalProperties: + type: string + body: + type: object + media: + type: array + items: + type: object + valueType: + allOf: + - $ref: '#/components/schemas/GoogleProtobufValue' + description: Description of value + repeatedValueType: + type: array + items: + $ref: '#/components/schemas/GoogleProtobufValue' + description: Description of repeated value + boolValueType: + type: boolean + bytesValueType: + type: string + format: bytes + int32ValueType: + type: integer + format: int32 + uint32ValueType: + type: integer + format: uint32 + stringValueType: + type: string + int64ValueType: + type: string + uint64ValueType: + type: string + floatValueType: + type: number + format: float + doubleValueType: + type: number + format: double + timestampType: + type: string + format: date-time + durationType: + pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ + type: string + description: The body of Messaging_CreateMessage Message_EmbMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_string_enum.yaml b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_string_enum.yaml index 1158893e..b2703e17 100644 --- a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_string_enum.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_string_enum.yaml @@ -177,7 +177,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Message' + $ref: '#/components/schemas/Message_Body' required: true responses: "200": @@ -459,6 +459,78 @@ components: durationType: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string + Message_Body: + type: object + properties: + stringType: + type: string + recursiveType: + $ref: '#/components/schemas/RecursiveParent' + embeddedType: + $ref: '#/components/schemas/Message_EmbMessage' + subType: + $ref: '#/components/schemas/SubMessage' + repeatedType: + type: array + items: + type: string + repeatedSubType: + type: array + items: + $ref: '#/components/schemas/SubMessage' + repeatedRecursiveType: + type: array + items: + $ref: '#/components/schemas/RecursiveParent' + mapType: + type: object + additionalProperties: + type: string + body: + type: object + media: + type: array + items: + type: object + valueType: + allOf: + - $ref: '#/components/schemas/GoogleProtobufValue' + description: Description of value + repeatedValueType: + type: array + items: + $ref: '#/components/schemas/GoogleProtobufValue' + description: Description of repeated value + boolValueType: + type: boolean + bytesValueType: + type: string + format: bytes + int32ValueType: + type: integer + format: int32 + uint32ValueType: + type: integer + format: uint32 + stringValueType: + type: string + int64ValueType: + type: string + uint64ValueType: + type: string + floatValueType: + type: number + format: float + doubleValueType: + type: number + format: double + timestampType: + type: string + format: date-time + durationType: + pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ + type: string + description: The body of Messaging_CreateMessage Message_EmbMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/generator/generator.go b/cmd/protoc-gen-openapi/generator/generator.go index e548ab21..97cd09b5 100644 --- a/cmd/protoc-gen-openapi/generator/generator.go +++ b/cmd/protoc-gen-openapi/generator/generator.go @@ -619,9 +619,16 @@ func (g *OpenAPIv3Generator) buildOperationV3( var requestSchema *v3.SchemaOrReference if bodyField == "*" { - // Pass the entire request message as the request body. - requestSchema = g.reflect.schemaOrReferenceForMessage(inputMessage.Desc) - + // The special name `*` can be used in the body mapping to define that + // every field not bound by the path template should be mapped to the + // request body. + if len(parameters) == 0 { + // Pass the entire request message as the request body. + requestSchema = g.reflect.schemaOrReferenceForMessage(inputMessage.Desc) + } else { + // Generate a request body schema without the coveredParameters + requestSchema = g.addSchemaForRequestBodyToDocumentV3(d, inputMessage, coveredParameters, "The body of "+operationID) + } } else { // If body refers to a message field, use that type. for _, field := range inputMessage.Fields { @@ -668,6 +675,35 @@ func (g *OpenAPIv3Generator) buildOperationV3( return op, path } +func (g *OpenAPIv3Generator) addSchemaForRequestBodyToDocumentV3(d *v3.Document, inputMessage *protogen.Message, ignoreFields []string, description string) *v3.SchemaOrReference { // We need to create a schema that does not include the path parameters, "The body of "+operationID. + schemaName := g.reflect.formatMessageName(inputMessage.Desc) + "_Body" + ref := "#/components/schemas/" + schemaName + + if !contains(g.reflect.requiredSchemas, schemaName) { + g.reflect.requiredSchemas = append(g.reflect.requiredSchemas, schemaName) + } + + fs := make([]*protogen.Field, len(inputMessage.Fields)) + copy(fs, inputMessage.Fields) + + for i := len(fs) - 1; i >= 0; i-- { + fieldName := string(fs[i].Desc.Name()) + if contains(ignoreFields, fieldName) { + // field is already covered by a path parameter + fs = append(fs[:i], fs[i+1:]...) + } + } + typeName := g.reflect.fullMessageTypeName(inputMessage.Desc) + g.addSchemaWithFieldsToDocumentV3(d, schemaName, fs, typeName+"_Body", description, inputMessage.Desc.Options()) + + requestSchema := &v3.SchemaOrReference{ + Oneof: &v3.SchemaOrReference_Reference{ + Reference: &v3.Reference{XRef: ref}, + }, + } + return requestSchema +} + // addOperationToDocumentV3 adds an operation to the specified path/method. func (g *OpenAPIv3Generator) addOperationToDocumentV3(d *v3.Document, op *v3.Operation, path string, methodName string) { var selectedPathItem *v3.NamedPathItem @@ -789,120 +825,124 @@ func (g *OpenAPIv3Generator) addSchemasForMessagesToDocumentV3(d *v3.Document, m } schemaName := g.reflect.formatMessageName(message.Desc) - - // Only generate this if we need it and haven't already generated it. - if !contains(g.reflect.requiredSchemas, schemaName) || - contains(g.generatedSchemas, schemaName) { - continue - } - typeName := g.reflect.fullMessageTypeName(message.Desc) messageDescription := g.filterCommentString(message.Comments.Leading) + opts := message.Desc.Options() + g.addSchemaWithFieldsToDocumentV3(d, schemaName, message.Fields, typeName, messageDescription, opts) + } +} - // `google.protobuf.Value` and `google.protobuf.Any` have special JSON transcoding - // so we can't just reflect on the message descriptor. - if typeName == ".google.protobuf.Value" { - g.addSchemaToDocumentV3(d, wk.NewGoogleProtobufValueSchema(schemaName)) - continue - } else if typeName == ".google.protobuf.Any" { - g.addSchemaToDocumentV3(d, wk.NewGoogleProtobufAnySchema(schemaName)) - continue - } else if typeName == ".google.rpc.Status" { - anySchemaName := g.reflect.formatMessageName(anyProtoDesc) - g.addSchemaToDocumentV3(d, wk.NewGoogleProtobufAnySchema(anySchemaName)) - g.addSchemaToDocumentV3(d, wk.NewGoogleRpcStatusSchema(schemaName, anySchemaName)) - continue - } +// addSchemaWithFieldsToDocumentV3 adds info +func (g *OpenAPIv3Generator) addSchemaWithFieldsToDocumentV3(d *v3.Document, schemaName string, fields []*protogen.Field, typeName string, messageDescription string, opts protoreflect.ProtoMessage) { + // Only generate this if we need it and haven't already generated it. + if !contains(g.reflect.requiredSchemas, schemaName) || + contains(g.generatedSchemas, schemaName) { + return + } - // Build an array holding the fields of the message. - definitionProperties := &v3.Properties{ - AdditionalProperties: make([]*v3.NamedSchemaOrReference, 0), - } - - var required []string - for _, field := range message.Fields { - // Get the field description from the comments. - description := g.filterCommentString(field.Comments.Leading) - // Check the field annotations to see if this is a readonly or writeonly field. - inputOnly := false - outputOnly := false - extension := proto.GetExtension(field.Desc.Options(), annotations.E_FieldBehavior) - if extension != nil { - switch v := extension.(type) { - case []annotations.FieldBehavior: - for _, vv := range v { - switch vv { - case annotations.FieldBehavior_OUTPUT_ONLY: - outputOnly = true - case annotations.FieldBehavior_INPUT_ONLY: - inputOnly = true - case annotations.FieldBehavior_REQUIRED: - required = append(required, g.reflect.formatFieldName(field.Desc)) - } + // `google.protobuf.Value` and `google.protobuf.Any` have special JSON transcoding + // so we can't just reflect on the message descriptor. + if typeName == ".google.protobuf.Value" { + g.addSchemaToDocumentV3(d, wk.NewGoogleProtobufValueSchema(schemaName)) + return + } else if typeName == ".google.protobuf.Any" { + g.addSchemaToDocumentV3(d, wk.NewGoogleProtobufAnySchema(schemaName)) + return + } else if typeName == ".google.rpc.Status" { + anySchemaName := g.reflect.formatMessageName(anyProtoDesc) + g.addSchemaToDocumentV3(d, wk.NewGoogleProtobufAnySchema(anySchemaName)) + g.addSchemaToDocumentV3(d, wk.NewGoogleRpcStatusSchema(schemaName, anySchemaName)) + return + } + + // Build an array holding the fields of the message. + definitionProperties := &v3.Properties{ + AdditionalProperties: make([]*v3.NamedSchemaOrReference, 0), + } + + var required []string + for _, field := range fields { + // Get the field description from the comments. + description := g.filterCommentString(field.Comments.Leading) + // Check the field annotations to see if this is a readonly or writeonly field. + inputOnly := false + outputOnly := false + extension := proto.GetExtension(field.Desc.Options(), annotations.E_FieldBehavior) + if extension != nil { + switch v := extension.(type) { + case []annotations.FieldBehavior: + for _, vv := range v { + switch vv { + case annotations.FieldBehavior_OUTPUT_ONLY: + outputOnly = true + case annotations.FieldBehavior_INPUT_ONLY: + inputOnly = true + case annotations.FieldBehavior_REQUIRED: + required = append(required, g.reflect.formatFieldName(field.Desc)) } - default: - log.Printf("unsupported extension type %T", extension) } + default: + log.Printf("unsupported extension type %T", extension) } + } - // The field is either described by a reference or a schema. - fieldSchema := g.reflect.schemaOrReferenceForField(field.Desc) - if fieldSchema == nil { - continue - } + // The field is either described by a reference or a schema. + fieldSchema := g.reflect.schemaOrReferenceForField(field.Desc) + if fieldSchema == nil { + continue + } - // If this field has siblings and is a $ref now, create a new schema use `allOf` to wrap it - wrapperNeeded := inputOnly || outputOnly || description != "" - if wrapperNeeded { - if _, ok := fieldSchema.Oneof.(*v3.SchemaOrReference_Reference); ok { - fieldSchema = &v3.SchemaOrReference{Oneof: &v3.SchemaOrReference_Schema{Schema: &v3.Schema{ - AllOf: []*v3.SchemaOrReference{fieldSchema}, - }}} - } + // If this field has siblings and is a $ref now, create a new schema use `allOf` to wrap it + wrapperNeeded := inputOnly || outputOnly || description != "" + if wrapperNeeded { + if _, ok := fieldSchema.Oneof.(*v3.SchemaOrReference_Reference); ok { + fieldSchema = &v3.SchemaOrReference{Oneof: &v3.SchemaOrReference_Schema{Schema: &v3.Schema{ + AllOf: []*v3.SchemaOrReference{fieldSchema}, + }}} } + } - if schema, ok := fieldSchema.Oneof.(*v3.SchemaOrReference_Schema); ok { - schema.Schema.Description = description - schema.Schema.ReadOnly = outputOnly - schema.Schema.WriteOnly = inputOnly + if schema, ok := fieldSchema.Oneof.(*v3.SchemaOrReference_Schema); ok { + schema.Schema.Description = description + schema.Schema.ReadOnly = outputOnly + schema.Schema.WriteOnly = inputOnly - // 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)) - } + // 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( - definitionProperties.AdditionalProperties, - &v3.NamedSchemaOrReference{ - Name: g.reflect.formatFieldName(field.Desc), - Value: fieldSchema, - }, - ) } - schema := &v3.Schema{ - Type: "object", - Description: messageDescription, - Properties: definitionProperties, - Required: required, - } + definitionProperties.AdditionalProperties = append( + definitionProperties.AdditionalProperties, + &v3.NamedSchemaOrReference{ + Name: g.reflect.formatFieldName(field.Desc), + Value: fieldSchema, + }, + ) + } - // 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)) - } + schema := &v3.Schema{ + Type: "object", + Description: messageDescription, + Properties: definitionProperties, + Required: required, + } - // Add the schema to the components.schema list. - g.addSchemaToDocumentV3(d, &v3.NamedSchemaOrReference{ - Name: schemaName, - Value: &v3.SchemaOrReference{ - Oneof: &v3.SchemaOrReference_Schema{ - Schema: schema, - }, - }, - }) + // Merge any `Schema` annotations with the current + extSchema := proto.GetExtension(opts, 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: schema, + }, + }, + }) } diff --git a/metrics/complexity.pb.go b/metrics/complexity.pb.go index 68b72341..1bf27583 100644 --- a/metrics/complexity.pb.go +++ b/metrics/complexity.pb.go @@ -14,8 +14,8 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.31.0 -// protoc v4.23.4 +// protoc-gen-go v1.34.1 +// protoc v4.24.3 // source: metrics/complexity.proto package gnostic_metrics_v1 diff --git a/metrics/vocabulary.pb.go b/metrics/vocabulary.pb.go index ffa22a17..f1748f3c 100644 --- a/metrics/vocabulary.pb.go +++ b/metrics/vocabulary.pb.go @@ -14,8 +14,8 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.31.0 -// protoc v4.23.4 +// protoc-gen-go v1.34.1 +// protoc v4.24.3 // source: metrics/vocabulary.proto package gnostic_metrics_v1 diff --git a/plugins/plugin.pb.go b/plugins/plugin.pb.go index ab870064..cb5f3225 100644 --- a/plugins/plugin.pb.go +++ b/plugins/plugin.pb.go @@ -22,8 +22,8 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.31.0 -// protoc v4.23.4 +// protoc-gen-go v1.34.1 +// protoc v4.24.3 // source: plugins/plugin.proto package gnostic_plugin_v1 diff --git a/surface/surface.pb.go b/surface/surface.pb.go index 4a799816..38cc9351 100644 --- a/surface/surface.pb.go +++ b/surface/surface.pb.go @@ -16,8 +16,8 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.31.0 -// protoc v4.23.4 +// protoc-gen-go v1.34.1 +// protoc v4.24.3 // source: surface/surface.proto package surface_v1