Skip to content

Commit

Permalink
Update protoc-gen-openapi to handle "unnamed" path parameters, json n…
Browse files Browse the repository at this point in the history
…aming, google.protobuf.Struct and google.protobuf.Empty (#261)

* Update protoc-gen-openapi to handle "unnamed" path parameters

Paths like this:
`/v1/users/{user_id}/messages/{message_id}`
is now being parsed into path parameters instead of query paramaters.

* Add support for protobuf.Struct and protobuf.Empty

Struct will be converted to type object.
Empty will caused the field to be completely ignored.

* Add support for protobuf.Struct as response

Will return an empty JSON object

* Add support for Struct and Empty requestBody and responseBody

Also better support for some google.protoby.* array types

* Enable flag for chosing json naming (camelCase)

- output raw protobuf names as default
- enable json naming with a flag
- use names from json_name options, json naming is enabled
- set version with a flag
- minor cleanup according to go vet

json naming and version is set with standard protoc key, values:
`--openapi_out=json=true,version=1.2.3:.`

Each test now also has a test with json naming enabled and a version set.

Testing was simplified a bit to utilize table-driven tests with sub tests.

* Use tags for grouping + add title and description as config params

A new tag is created per services and added to each of it's operations.

The title and description has been added as standard params and can be set as key value params with either --openapi_out or --openapi_opt.
E.g.:
--openapi_opt=title=API,description="testing testing",version=1.2.3
--openapi_out=json=true:.

Title and description of the document is handled a bit differently:
 - A service's name now becomes it's tag
 - Comments will be used for description instead of summary
 - If doc title and description is set, it is never overwritten
 - If only one service is being parsed:
    - If doc has no title, use the service's tag name + " API"
    - If doc has no description, use service tag description
    - Remove the tag description

* Sort tags

* Replace all instances of "googleapis/gnostic" with "google/gnostic"

* Rename openapi.json.yaml test files to openapi_json.yaml

* Change JSONNames parameter to Naming and make json default

This enables different naming schemes.
Initially supports json and proto with json as default.
json format is camelCase, which seems to be the unofficial standard throughout proto + json projects like Envoy, gRPC Gateway, etc.

* Adjust "json-style" naming in protoc-gen-openapi to capitalize schema names.

This aligns protoc-gen-openapi output with the Google API Discovery Document
convention of capitalizing schema names. This capitalization is also in
OpenAPI descriptions translated from Discovery Docs, for example:

https://github.com/APIs-guru/openapi-directory/blob/16231c13a2041427a3f799a4d8053cb6cb9be4a8/APIs/googleapis.com/translate/v3/openapi.yaml#L742

(a very minor typo is also fixed in this commit)

Co-authored-by: Tim Burks <timburks@google.com>
  • Loading branch information
morphar and timburks authored Nov 23, 2021
1 parent bed1336 commit 6215ae6
Show file tree
Hide file tree
Showing 18 changed files with 1,560 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@

openapi: 3.0.3
info:
title: LibraryService
description: 'This API represents a simple digital library. It lets you manage Shelf resources and Book resources in the library. It defines the following resource model: - The API has a collection of [Shelf][google.example.library.v1.Shelf] resources, named `shelves/*` - Each Shelf has a collection of [Book][google.example.library.v1.Book] resources, named `shelves/*/books/*`'
title: LibraryService API
description: |-
This API represents a simple digital library. It lets you manage Shelf
resources and Book resources in the library. It defines the following
resource model:
- The API has a collection of [Shelf][google.example.library.v1.Shelf]
resources, named `shelves/*`
- Each Shelf has a collection of [Book][google.example.library.v1.Book]
resources, named `shelves/*/books/*`
version: 0.0.1
paths:
/v1/shelves:
get:
summary: Lists shelves. The order is unspecified but deterministic. Newly created shelves will not necessarily be added to the end of this list.
tags:
- LibraryService
description: |-
Lists shelves. The order is unspecified but deterministic. Newly created
shelves will not necessarily be added to the end of this list.
operationId: LibraryService_ListShelves
parameters:
- name: page_size
Expand All @@ -30,7 +43,9 @@ paths:
schema:
$ref: '#/components/schemas/ListShelvesResponse'
post:
summary: Creates a shelf, and returns the new Shelf.
tags:
- LibraryService
description: Creates a shelf, and returns the new Shelf.
operationId: LibraryService_CreateShelf
requestBody:
content:
Expand All @@ -47,7 +62,9 @@ paths:
$ref: '#/components/schemas/Shelf'
/v1/shelves/{shelf}:
get:
summary: Gets a shelf. Returns NOT_FOUND if the shelf does not exist.
tags:
- LibraryService
description: Gets a shelf. Returns NOT_FOUND if the shelf does not exist.
operationId: LibraryService_GetShelf
parameters:
- name: shelf
Expand All @@ -64,7 +81,9 @@ paths:
schema:
$ref: '#/components/schemas/Shelf'
delete:
summary: Deletes a shelf. Returns NOT_FOUND if the shelf does not exist.
tags:
- LibraryService
description: Deletes a shelf. Returns NOT_FOUND if the shelf does not exist.
operationId: LibraryService_DeleteShelf
parameters:
- name: shelf
Expand All @@ -79,7 +98,12 @@ paths:
content: {}
/v1/shelves/{shelf}/books:
get:
summary: Lists books in a shelf. The order is unspecified but deterministic. Newly created books will not necessarily be added to the end of this list. Returns NOT_FOUND if the shelf does not exist.
tags:
- LibraryService
description: |-
Lists books in a shelf. The order is unspecified but deterministic. Newly
created books will not necessarily be added to the end of this list.
Returns NOT_FOUND if the shelf does not exist.
operationId: LibraryService_ListBooks
parameters:
- name: shelf
Expand All @@ -106,7 +130,9 @@ paths:
schema:
$ref: '#/components/schemas/ListBooksResponse'
post:
summary: Creates a book, and returns the new Book.
tags:
- LibraryService
description: Creates a book, and returns the new Book.
operationId: LibraryService_CreateBook
parameters:
- name: shelf
Expand All @@ -130,7 +156,9 @@ paths:
$ref: '#/components/schemas/Book'
/v1/shelves/{shelf}/books/{book}:
get:
summary: Gets a book. Returns NOT_FOUND if the book does not exist.
tags:
- LibraryService
description: Gets a book. Returns NOT_FOUND if the book does not exist.
operationId: LibraryService_GetBook
parameters:
- name: shelf
Expand All @@ -153,7 +181,11 @@ paths:
schema:
$ref: '#/components/schemas/Book'
put:
summary: Updates a book. Returns INVALID_ARGUMENT if the name of the book is non-empty and does not equal the existing name.
tags:
- LibraryService
description: |-
Updates a book. Returns INVALID_ARGUMENT if the name of the book
is non-empty and does not equal the existing name.
operationId: LibraryService_UpdateBook
parameters:
- name: shelf
Expand Down Expand Up @@ -187,7 +219,9 @@ paths:
schema:
$ref: '#/components/schemas/Book'
delete:
summary: Deletes a book. Returns NOT_FOUND if the book does not exist.
tags:
- LibraryService
description: Deletes a book. Returns NOT_FOUND if the book does not exist.
operationId: LibraryService_DeleteBook
parameters:
- name: shelf
Expand All @@ -208,7 +242,11 @@ paths:
content: {}
/v1/shelves/{shelf}/books/{book}:move:
post:
summary: Moves a book to another shelf, and returns the new book. The book id of the new book may not be the same as the original book.
tags:
- LibraryService
description: |-
Moves a book to another shelf, and returns the new book. The book
id of the new book may not be the same as the original book.
operationId: LibraryService_MoveBook
parameters:
- name: shelf
Expand Down Expand Up @@ -238,7 +276,16 @@ paths:
$ref: '#/components/schemas/Book'
/v1/shelves/{shelf}:merge:
post:
summary: Merges two shelves by adding all books from the shelf named `other_shelf_name` to shelf `name`, and deletes `other_shelf_name`. Returns the updated shelf. The book ids of the moved books may not be the same as the original books. Returns NOT_FOUND if either shelf does not exist. This call is a no-op if the specified shelves are the same.
tags:
- LibraryService
description: |-
Merges two shelves by adding all books from the shelf named
`other_shelf_name` to shelf `name`, and deletes
`other_shelf_name`. Returns the updated shelf.
The book ids of the moved books may not be the same as the original books.
Returns NOT_FOUND if either shelf does not exist.
This call is a no-op if the specified shelves are the same.
operationId: LibraryService_MergeShelves
parameters:
- name: shelf
Expand Down Expand Up @@ -356,3 +403,5 @@ components:
description: The last update date and time.
format: date-time
description: A Shelf contains a collection of books with a theme.
tags:
- name: LibraryService
Loading

0 comments on commit 6215ae6

Please sign in to comment.