From 2d266224e8908e22d908a2d93d2dd5c8c2ca1537 Mon Sep 17 00:00:00 2001 From: "esraa.ali" Date: Sun, 15 Oct 2023 22:17:51 +0200 Subject: [PATCH] feat: Support format for 64-bit integer field types --- .../tests/protobuftypes/message.proto | 6 ++ .../examples/tests/protobuftypes/openapi.yaml | 65 +++++++++++++++++++ .../openapi_default_response.yaml | 65 +++++++++++++++++++ .../openapi_fq_schema_naming.yaml | 65 +++++++++++++++++++ .../tests/protobuftypes/openapi_json.yaml | 65 +++++++++++++++++++ .../protobuftypes/openapi_string_enum.yaml | 65 +++++++++++++++++++ cmd/protoc-gen-openapi/generator/reflector.go | 2 +- .../generator/wellknown/schemas.go | 6 ++ 8 files changed, 338 insertions(+), 1 deletion(-) diff --git a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/message.proto b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/message.proto index 51160a14..2df0044c 100644 --- a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/message.proto +++ b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/message.proto @@ -115,4 +115,10 @@ message Message { google.protobuf.DoubleValue double_value_type = 23; google.protobuf.Timestamp timestamp_type = 24; google.protobuf.Duration duration_type = 25; + // Description of 64-bit integer values + int64 int64_type = 26; + sint64 sint64_type = 27; + uint64 uint64_type = 28; + fixed64 fixed64_type = 29; + sfixed64 sfixed64_type = 30; } diff --git a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi.yaml b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi.yaml index 6cb6b7fb..0f2e68fe 100644 --- a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi.yaml @@ -150,6 +150,31 @@ paths: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string description: Represents a a duration between -315,576,000,000s and 315,576,000,000s (around 10000 years). Precision is in nanoseconds. 1 nanosecond is represented as 0.000000001s + - name: int64_type + in: query + schema: + type: string + format: int64 + - name: sint64_type + in: query + schema: + type: string + format: sint64 + - name: uint64_type + in: query + schema: + type: string + format: uint64 + - name: fixed64_type + in: query + schema: + type: string + format: fixed64 + - name: sfixed64_type + in: query + schema: + type: string + format: sfixed64 responses: "200": description: OK @@ -315,6 +340,31 @@ paths: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string description: Represents a a duration between -315,576,000,000s and 315,576,000,000s (around 10000 years). Precision is in nanoseconds. 1 nanosecond is represented as 0.000000001s + - name: int64_type + in: query + schema: + type: string + format: int64 + - name: sint64_type + in: query + schema: + type: string + format: sint64 + - name: uint64_type + in: query + schema: + type: string + format: uint64 + - name: fixed64_type + in: query + schema: + type: string + format: fixed64 + - name: sfixed64_type + in: query + schema: + type: string + format: sfixed64 requestBody: content: application/json: @@ -459,6 +509,21 @@ components: duration_type: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string + int64_type: + type: string + format: int64 + sint64_type: + type: string + format: sint64 + uint64_type: + type: string + format: uint64 + fixed64_type: + type: string + format: fixed64 + sfixed64_type: + type: string + format: sfixed64 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..2c1780a7 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 @@ -150,6 +150,31 @@ paths: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string description: Represents a a duration between -315,576,000,000s and 315,576,000,000s (around 10000 years). Precision is in nanoseconds. 1 nanosecond is represented as 0.000000001s + - name: int64Type + in: query + schema: + type: string + format: int64 + - name: sint64Type + in: query + schema: + type: string + format: sint64 + - name: uint64Type + in: query + schema: + type: string + format: uint64 + - name: fixed64Type + in: query + schema: + type: string + format: fixed64 + - name: sfixed64Type + in: query + schema: + type: string + format: sfixed64 responses: "200": description: OK @@ -315,6 +340,31 @@ paths: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string description: Represents a a duration between -315,576,000,000s and 315,576,000,000s (around 10000 years). Precision is in nanoseconds. 1 nanosecond is represented as 0.000000001s + - name: int64Type + in: query + schema: + type: string + format: int64 + - name: sint64Type + in: query + schema: + type: string + format: sint64 + - name: uint64Type + in: query + schema: + type: string + format: uint64 + - name: fixed64Type + in: query + schema: + type: string + format: fixed64 + - name: sfixed64Type + in: query + schema: + type: string + format: sfixed64 requestBody: content: application/json: @@ -459,6 +509,21 @@ components: durationType: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string + int64Type: + type: string + format: int64 + sint64Type: + type: string + format: sint64 + uint64Type: + type: string + format: uint64 + fixed64Type: + type: string + format: fixed64 + sfixed64Type: + type: string + format: sfixed64 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..7ef586a4 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 @@ -150,6 +150,31 @@ paths: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string description: Represents a a duration between -315,576,000,000s and 315,576,000,000s (around 10000 years). Precision is in nanoseconds. 1 nanosecond is represented as 0.000000001s + - name: int64Type + in: query + schema: + type: string + format: int64 + - name: sint64Type + in: query + schema: + type: string + format: sint64 + - name: uint64Type + in: query + schema: + type: string + format: uint64 + - name: fixed64Type + in: query + schema: + type: string + format: fixed64 + - name: sfixed64Type + in: query + schema: + type: string + format: sfixed64 responses: "200": description: OK @@ -315,6 +340,31 @@ paths: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string description: Represents a a duration between -315,576,000,000s and 315,576,000,000s (around 10000 years). Precision is in nanoseconds. 1 nanosecond is represented as 0.000000001s + - name: int64Type + in: query + schema: + type: string + format: int64 + - name: sint64Type + in: query + schema: + type: string + format: sint64 + - name: uint64Type + in: query + schema: + type: string + format: uint64 + - name: fixed64Type + in: query + schema: + type: string + format: fixed64 + - name: sfixed64Type + in: query + schema: + type: string + format: sfixed64 requestBody: content: application/json: @@ -475,6 +525,21 @@ components: durationType: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string + int64Type: + type: string + format: int64 + sint64Type: + type: string + format: sint64 + uint64Type: + type: string + format: uint64 + fixed64Type: + type: string + format: fixed64 + sfixed64Type: + type: string + format: sfixed64 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..dde70f2f 100644 --- a/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_json.yaml +++ b/cmd/protoc-gen-openapi/examples/tests/protobuftypes/openapi_json.yaml @@ -150,6 +150,31 @@ paths: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string description: Represents a a duration between -315,576,000,000s and 315,576,000,000s (around 10000 years). Precision is in nanoseconds. 1 nanosecond is represented as 0.000000001s + - name: int64Type + in: query + schema: + type: string + format: int64 + - name: sint64Type + in: query + schema: + type: string + format: sint64 + - name: uint64Type + in: query + schema: + type: string + format: uint64 + - name: fixed64Type + in: query + schema: + type: string + format: fixed64 + - name: sfixed64Type + in: query + schema: + type: string + format: sfixed64 responses: "200": description: OK @@ -315,6 +340,31 @@ paths: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string description: Represents a a duration between -315,576,000,000s and 315,576,000,000s (around 10000 years). Precision is in nanoseconds. 1 nanosecond is represented as 0.000000001s + - name: int64Type + in: query + schema: + type: string + format: int64 + - name: sint64Type + in: query + schema: + type: string + format: sint64 + - name: uint64Type + in: query + schema: + type: string + format: uint64 + - name: fixed64Type + in: query + schema: + type: string + format: fixed64 + - name: sfixed64Type + in: query + schema: + type: string + format: sfixed64 requestBody: content: application/json: @@ -459,6 +509,21 @@ components: durationType: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string + int64Type: + type: string + format: int64 + sint64Type: + type: string + format: sint64 + uint64Type: + type: string + format: uint64 + fixed64Type: + type: string + format: fixed64 + sfixed64Type: + type: string + format: sfixed64 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..2c1780a7 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 @@ -150,6 +150,31 @@ paths: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string description: Represents a a duration between -315,576,000,000s and 315,576,000,000s (around 10000 years). Precision is in nanoseconds. 1 nanosecond is represented as 0.000000001s + - name: int64Type + in: query + schema: + type: string + format: int64 + - name: sint64Type + in: query + schema: + type: string + format: sint64 + - name: uint64Type + in: query + schema: + type: string + format: uint64 + - name: fixed64Type + in: query + schema: + type: string + format: fixed64 + - name: sfixed64Type + in: query + schema: + type: string + format: sfixed64 responses: "200": description: OK @@ -315,6 +340,31 @@ paths: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string description: Represents a a duration between -315,576,000,000s and 315,576,000,000s (around 10000 years). Precision is in nanoseconds. 1 nanosecond is represented as 0.000000001s + - name: int64Type + in: query + schema: + type: string + format: int64 + - name: sint64Type + in: query + schema: + type: string + format: sint64 + - name: uint64Type + in: query + schema: + type: string + format: uint64 + - name: fixed64Type + in: query + schema: + type: string + format: fixed64 + - name: sfixed64Type + in: query + schema: + type: string + format: sfixed64 requestBody: content: application/json: @@ -459,6 +509,21 @@ components: durationType: pattern: ^-?(?:0|[1-9][0-9]{0,11})(?:\.[0-9]{1,9})?s$ type: string + int64Type: + type: string + format: int64 + sint64Type: + type: string + format: sint64 + uint64Type: + type: string + format: uint64 + fixed64Type: + type: string + format: fixed64 + sfixed64Type: + type: string + format: sfixed64 Message_EmbMessage: type: object properties: diff --git a/cmd/protoc-gen-openapi/generator/reflector.go b/cmd/protoc-gen-openapi/generator/reflector.go index 31a0f930..a328c870 100644 --- a/cmd/protoc-gen-openapi/generator/reflector.go +++ b/cmd/protoc-gen-openapi/generator/reflector.go @@ -213,7 +213,7 @@ func (r *OpenAPIv3Reflector) schemaOrReferenceForField(field protoreflect.FieldD case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Uint64Kind, protoreflect.Sfixed64Kind, protoreflect.Fixed64Kind: - kindSchema = wk.NewStringSchema() + kindSchema = wk.NewInt64Schema(kind.String()) case protoreflect.EnumKind: kindSchema = wk.NewEnumSchema(*&r.conf.EnumType, field) diff --git a/cmd/protoc-gen-openapi/generator/wellknown/schemas.go b/cmd/protoc-gen-openapi/generator/wellknown/schemas.go index 8840dde8..11b9c88c 100644 --- a/cmd/protoc-gen-openapi/generator/wellknown/schemas.go +++ b/cmd/protoc-gen-openapi/generator/wellknown/schemas.go @@ -50,6 +50,12 @@ func NewNumberSchema(format string) *v3.SchemaOrReference { Schema: &v3.Schema{Type: "number", Format: format}}} } +func NewInt64Schema(format string) *v3.SchemaOrReference { + return &v3.SchemaOrReference{ + Oneof: &v3.SchemaOrReference_Schema{ + Schema: &v3.Schema{Type: "string", Format: format}}} +} + func NewEnumSchema(enum_type *string, field protoreflect.FieldDescriptor) *v3.SchemaOrReference { schema := &v3.Schema{Format: "enum"} if enum_type != nil && *enum_type == "string" {