Skip to content

Commit 56dc3e0

Browse files
author
Josh
committed
Allow field type overrides
1 parent 8471760 commit 56dc3e0

File tree

8 files changed

+118
-43
lines changed

8 files changed

+118
-43
lines changed

api/oapi/v1/field.pb.go

Lines changed: 73 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/oapi/v1/field.proto

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,15 @@ message FieldOptions {
6262

6363
// Values that value must be one of.
6464
repeated string enum = 14;
65+
66+
// The format of the value.
67+
optional string format = 15;
68+
69+
// Marks the field as required.
70+
bool required = 16;
71+
72+
// Use the specified type instead of the defined type. This is useful when you
73+
// are using something like google.protobuf.Timestamp and it really should be
74+
// a string.
75+
optional string as_type = 17;
6576
}

internal/generator/path.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ func (g *Generator) parseParameters(in, path string, parameters []*oapiv1.Parame
259259
}
260260

261261
if parameter.Options != nil {
262-
err := setProperties(paramRef.Value.Schema.Value, parameter.Options)
262+
err := setProperties(paramRef.Value.Schema.Value, parameter.Options, func() {
263+
paramRef.Value.Required = true
264+
})
263265
if err != nil {
264266
return nil, err
265267
}

internal/generator/schema.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (g *Generator) getPackageSchema(pkg, name string) string {
268268
return path.Join(prefix, pkg+"."+name)
269269
}
270270

271-
func setProperties(s *openapi3.Schema, fo *oapiv1.FieldOptions) error {
271+
func setProperties(s *openapi3.Schema, fo *oapiv1.FieldOptions, requiredFn func()) error {
272272
s.Min = fo.Min
273273
s.Max = fo.Max
274274

@@ -316,6 +316,18 @@ func setProperties(s *openapi3.Schema, fo *oapiv1.FieldOptions) error {
316316
}
317317
}
318318

319+
if fo.Format != nil {
320+
s.Format = *fo.Format
321+
}
322+
323+
if fo.AsType != nil {
324+
s.Type = *fo.AsType
325+
}
326+
327+
if requiredFn != nil && fo.Required {
328+
requiredFn()
329+
}
330+
319331
return nil
320332
}
321333

@@ -328,5 +340,7 @@ func (g *Generator) setSchemaProperties(s *openapi3.Schema, parent *openapi3.Sch
328340

329341
fo := extOptions.(*oapiv1.FieldOptions)
330342

331-
return setProperties(s, fo)
343+
return setProperties(s, fo, func() {
344+
parent.Required = append(parent.Required, g.getFieldName(field))
345+
})
332346
}

main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"flag"
5-
65
"github.com/technicallyjosh/protoc-gen-openapi/internal/generator"
76
"google.golang.org/protobuf/compiler/protogen"
87
)

test/basic_test_openapi.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ info:
33
description: test description
44
title: test title
55
version: 1.1.0
6-
paths: {}
6+
paths: { }
77
components:
88
responses:
99
default:
@@ -19,4 +19,3 @@ components:
1919
type: string
2020
msg:
2121
type: string
22-

test/field_test.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "oapi/v1/file.proto";
66
import "oapi/v1/service.proto";
77
import "oapi/v1/method.proto";
88
import "oapi/v1/field.proto";
9+
import "google/protobuf/timestamp.proto";
910

1011
option go_package = "github.com/technicallyjosh/protoc-gen-openapi/test_api";
1112

@@ -37,6 +38,7 @@ service TestService {
3738
message MessageRequest {
3839
string string = 1;
3940
string required_string = 2 [(oapi.v1.required) = true];
41+
string required_string2 = 3 [(oapi.v1.options).required = true];
4042
}
4143

4244
message TestFieldTypesRequest {
@@ -55,6 +57,12 @@ message TestFieldTypesRequest {
5557
repeated string repeated_string = 7;
5658
repeated Message repeated_message = 8;
5759
repeated MessageRequest repeated_request = 9;
60+
google.protobuf.Timestamp message_at = 10 [
61+
(oapi.v1.options) = {
62+
as_type: "string"
63+
format: "date-time"
64+
}
65+
];
5866
}
5967

6068
message TestFieldTypesResponse {}

test/field_test_openapi.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ paths:
4848
type: string
4949
required_string:
5050
type: string
51+
required_string2:
52+
type: string
5153
required:
5254
- required_string
55+
- required_string2
56+
message_at:
57+
type: string
58+
format: date-time
5359

5460
responses:
5561
"200":

0 commit comments

Comments
 (0)