Skip to content

Commit 2cb2e85

Browse files
author
Josh
committed
Add enum to field options
1 parent e3001c4 commit 2cb2e85

File tree

6 files changed

+70
-41
lines changed

6 files changed

+70
-41
lines changed

api/oapi/v1/field.pb.go

Lines changed: 47 additions & 36 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,7 @@ message FieldOptions {
5959

6060
// The number that the value must be a multiple of.
6161
optional double multiple_of = 13;
62+
63+
// Values that value must be one of.
64+
repeated string enum = 14;
6265
}

api/oapi/v1/security.pb.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/generator/schema.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ func setProperties(s *openapi3.Schema, fo *oapiv1.FieldOptions) error {
308308

309309
s.MultipleOf = fo.MultipleOf
310310

311+
if len(fo.Enum) > 0 {
312+
s.Enum = make([]any, 0)
313+
314+
for i := range fo.Enum {
315+
s.Enum = append(s.Enum, fo.Enum[i])
316+
}
317+
}
318+
311319
return nil
312320
}
313321

test/field_test.proto

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ message TestFieldTypesRequest {
4444
string string = 1;
4545
}
4646

47-
string string = 1;
47+
string string = 1 [(oapi.v1.options) = {
48+
enum: ["test"]
49+
}];
4850
bool bool = 2;
4951
int32 int32 = 3;
5052
int64 int64 = 4;

test/field_test_openapi.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ paths:
1616
properties:
1717
string:
1818
type: string
19+
enum:
20+
- test
1921
bool:
2022
type: boolean
2123
int32:
2224
type: integer
2325
int64:
2426
type: string
2527
uint32:
26-
type: integer
28+
type: integer
2729
uint64:
2830
type: string
2931
repeated_string:
@@ -54,7 +56,7 @@ paths:
5456
content:
5557
application/json:
5658
schema:
57-
properties: {}
59+
properties: { }
5860
description: ""
5961
default:
6062
$ref: '#/components/responses/default'
@@ -79,7 +81,7 @@ paths:
7981
content:
8082
application/json:
8183
schema:
82-
properties: {}
84+
properties: { }
8385
description: ""
8486
default:
8587
$ref: '#/components/responses/default'

0 commit comments

Comments
 (0)