Skip to content

Commit

Permalink
protoc-gen-openapi: timestamps should be serialized as strings (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
timburks authored Dec 3, 2020
1 parent eec9d28 commit 43bcb37
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import "google/api/client.proto";
import "google/api/field_behavior.proto";
import "google/api/resource.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";

option go_package = "google.golang.org/genproto/googleapis/example/library/v1;library";
option java_multiple_files = true;
Expand Down Expand Up @@ -165,6 +166,14 @@ message Book {

// Value indicating whether the book has been read.
bool read = 4;

// Creation timestamp.
google.protobuf.Timestamp create_time = 5
[(google.api.field_behavior) = OUTPUT_ONLY];

// Last update timestamp.
google.protobuf.Timestamp update_time = 6
[(google.api.field_behavior) = OUTPUT_ONLY];
}

// A Shelf contains a collection of books with a theme.
Expand All @@ -183,6 +192,15 @@ message Shelf {

// The theme of the shelf
string theme = 2;

// Creation timestamp.
google.protobuf.Timestamp create_time = 3
[(google.api.field_behavior) = OUTPUT_ONLY];

// Last update timestamp.
google.protobuf.Timestamp update_time = 4
[(google.api.field_behavior) = OUTPUT_ONLY];

}

// Request message for LibraryService.CreateShelf.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@ components:
read:
type: boolean
description: Value indicating whether the book has been read.
create_time:
readOnly: true
type: string
description: Creation timestamp.
format: RFC3339
update_time:
readOnly: true
type: string
description: Last update timestamp.
format: RFC3339
description: A single book in the library.
Empty:
properties: {}
Expand Down Expand Up @@ -334,4 +344,14 @@ components:
theme:
type: string
description: The theme of the shelf
create_time:
readOnly: true
type: string
description: Creation timestamp.
format: RFC3339
update_time:
readOnly: true
type: string
description: Last update timestamp.
format: RFC3339
description: A Shelf contains a collection of books with a theme.
11 changes: 9 additions & 2 deletions apps/protoc-gen-openapi/generator/openapi-v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,15 @@ func (g *OpenAPIv3Generator) addSchemasToDocumentV3(d *v3.Document, file *protog
k := field.Desc.Kind()
switch k {
case protoreflect.MessageKind:
// The field is described by a reference.
XRef = g.schemaReferenceForTypeName(fullMessageTypeName(field.Message))
typeName := fullMessageTypeName(field.Message)
if typeName == ".google.protobuf.Timestamp" {
// Timestamps are serialized as strings
fieldSchema.Type = "string"
fieldSchema.Format = "RFC3339"
} else {
// The field is described by a reference.
XRef = g.schemaReferenceForTypeName(typeName)
}
case protoreflect.StringKind:
fieldSchema.Type = "string"
case protoreflect.Int32Kind,
Expand Down

0 comments on commit 43bcb37

Please sign in to comment.