Skip to content

Commit b9bc400

Browse files
author
Josh Newman
committed
Fix object array and add tests
1 parent ff58a78 commit b9bc400

File tree

8 files changed

+208
-13
lines changed

8 files changed

+208
-13
lines changed

internal/generator/schema.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (g *Generator) buildSchema(doc *openapi3.T, message *protogen.Message, pare
8383
fieldSchemaRef.Value.Example = example
8484
}
8585

86-
// Try to set deprecated
86+
// Deprecated option.
8787
if standardOptions, ok := field.Desc.Options().(*descriptorpb.FieldOptions); ok {
8888
fieldSchemaRef.Value.Deprecated = standardOptions.GetDeprecated()
8989
}
@@ -172,7 +172,7 @@ func (g *Generator) buildSchema(doc *openapi3.T, message *protogen.Message, pare
172172
msg := allMessages.Get(fieldMessageName)
173173
if msg != nil {
174174
// Use the message to build it out inline instead of using a ref.
175-
err := g.buildSchema(doc, msg, fieldSchemaRef)
175+
err := g.buildSchema(doc, msg, fieldSchemaRef.Value.Items)
176176
if err != nil {
177177
return err
178178
}
@@ -223,7 +223,7 @@ func newSchemaRef(name string) string {
223223
func newFieldSchema(field protoreflect.FieldDescriptor) *openapi3.Schema {
224224
kind := field.Kind()
225225
schema := &openapi3.Schema{
226-
Type: protoKindToAPIType(field.Kind()),
226+
Type: protoKindToAPIType(kind),
227227
Properties: make(openapi3.Schemas),
228228
}
229229

@@ -238,11 +238,14 @@ func newFieldSchema(field protoreflect.FieldDescriptor) *openapi3.Schema {
238238
// protoKindToAPIType returns an OAPI type based on the proto kind sent.
239239
func protoKindToAPIType(kind protoreflect.Kind) string {
240240
switch kind {
241-
case protoreflect.StringKind:
241+
case protoreflect.StringKind,
242+
protoreflect.Int64Kind,
243+
protoreflect.Uint64Kind,
244+
protoreflect.Sint64Kind:
242245
return openapi3.TypeString
243-
case protoreflect.Int32Kind, protoreflect.Int64Kind,
244-
protoreflect.Uint32Kind, protoreflect.Uint64Kind,
245-
protoreflect.Sint32Kind, protoreflect.Sint64Kind:
246+
case protoreflect.Int32Kind,
247+
protoreflect.Uint32Kind,
248+
protoreflect.Sint32Kind:
246249
return openapi3.TypeInteger
247250
case protoreflect.BoolKind:
248251
return openapi3.TypeBoolean

main_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func (s *TestSuite) BeforeTest(suite, name string) {
4545
filename = "service_test.proto"
4646
case "TestMethod":
4747
filename = "method_test.proto"
48+
case "TestField":
49+
filename = "field_test.proto"
4850
default:
4951
s.FailNow("invalid test name")
5052
}
@@ -72,8 +74,6 @@ func (s *TestSuite) BeforeTest(suite, name string) {
7274
if err != nil {
7375
s.FailNow(err.Error())
7476
}
75-
76-
fmt.Println(string(s.rawDoc))
7777
}
7878

7979
func (s *TestSuite) TestBasic() {
@@ -92,6 +92,10 @@ func (s *TestSuite) TestMethod() {
9292
s.YAMLEq(readFile("method_test_openapi.yaml"), string(s.rawDoc))
9393
}
9494

95+
func (s *TestSuite) TestField() {
96+
s.YAMLEq(readFile("field_test_openapi.yaml"), string(s.rawDoc))
97+
}
98+
9599
func TestMain(t *testing.T) {
96100
suite.Run(t, new(TestSuite))
97101
}

test/basic_test_openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
openapi: 3.1.0
1+
openapi: 3.0.3
22
info:
33
description: test description
44
title: test title

test/field_test.proto

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
syntax = "proto3";
2+
3+
package test.api;
4+
5+
import "oapi/v1/file.proto";
6+
import "oapi/v1/service.proto";
7+
import "oapi/v1/method.proto";
8+
import "oapi/v1/field.proto";
9+
10+
option go_package = "github.com/technicallyjosh/protoc-gen-openapi/test_api";
11+
12+
option (oapi.v1.file) = {
13+
host: "swagger.io"
14+
prefix: "/v1"
15+
};
16+
17+
service TestService {
18+
option (oapi.v1.service) = {
19+
host: "api.swagger.io"
20+
x_display_name: "Test Service"
21+
x_tag_group: "Test Group"
22+
};
23+
24+
rpc TestFieldTypes(TestFieldTypesRequest) returns (TestFieldTypesResponse) {
25+
option (oapi.v1.method) = {
26+
post: "TestFieldTypes"
27+
};
28+
};
29+
30+
rpc TestFieldExamples(TestFieldExamplesRequest) returns (TestFieldExamplesResponse) {
31+
option (oapi.v1.method) = {
32+
post: "TestFieldExamples"
33+
};
34+
}
35+
}
36+
37+
message MessageRequest {
38+
string string = 1;
39+
string required_string = 2 [(oapi.v1.required) = true];
40+
}
41+
42+
message TestFieldTypesRequest {
43+
message Message {
44+
string string = 1;
45+
}
46+
47+
string string = 1;
48+
bool bool = 2;
49+
int32 int32 = 3;
50+
int64 int64 = 4;
51+
uint32 uint32 = 5;
52+
uint64 uint64 = 6;
53+
repeated string repeated_string = 7;
54+
repeated Message repeated_message = 8;
55+
repeated MessageRequest repeated_request = 9;
56+
}
57+
58+
message TestFieldTypesResponse {}
59+
60+
message TestFieldExamplesRequest {
61+
// Example: test-string
62+
string string = 1;
63+
}
64+
65+
message TestFieldExamplesResponse {}
66+
67+
message Error {
68+
string code = 1;
69+
string msg = 2;
70+
}

test/field_test_openapi.yaml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
openapi: 3.0.3
2+
3+
info:
4+
description: test description
5+
title: test title
6+
version: 1.1.0
7+
8+
paths:
9+
/v1/TestFieldTypes:
10+
post:
11+
operationId: TestService_TestFieldTypes
12+
requestBody:
13+
content:
14+
application/json:
15+
schema:
16+
properties:
17+
string:
18+
type: string
19+
bool:
20+
type: boolean
21+
int32:
22+
type: integer
23+
int64:
24+
type: string
25+
uint32:
26+
type: integer
27+
uint64:
28+
type: string
29+
repeated_string:
30+
items:
31+
type: string
32+
type: array
33+
repeated_message:
34+
items:
35+
properties:
36+
string:
37+
type: string
38+
type: object
39+
type: array
40+
repeated_request:
41+
type: array
42+
items:
43+
type: object
44+
properties:
45+
string:
46+
type: string
47+
required_string:
48+
type: string
49+
required:
50+
- required_string
51+
52+
responses:
53+
"200":
54+
content:
55+
application/json:
56+
schema:
57+
properties: {}
58+
description: ""
59+
default:
60+
$ref: '#/components/responses/default'
61+
servers:
62+
- url: https://api.swagger.io
63+
tags:
64+
- TestService
65+
66+
/v1/TestFieldExamples:
67+
post:
68+
operationId: TestService_TestFieldExamples
69+
requestBody:
70+
content:
71+
application/json:
72+
schema:
73+
properties:
74+
string:
75+
example: test-string
76+
type: string
77+
responses:
78+
"200":
79+
content:
80+
application/json:
81+
schema:
82+
properties: {}
83+
description: ""
84+
default:
85+
$ref: '#/components/responses/default'
86+
servers:
87+
- url: https://api.swagger.io
88+
tags:
89+
- TestService
90+
91+
components:
92+
responses:
93+
default:
94+
content:
95+
application/json:
96+
schema:
97+
$ref: '#/components/schemas/test.api.Error'
98+
description: ""
99+
schemas:
100+
test.api.Error:
101+
properties:
102+
code:
103+
type: string
104+
msg:
105+
type: string
106+
107+
servers:
108+
- url: https://swagger.io
109+
- url: https://api.swagger.io
110+
111+
tags:
112+
- name: TestService
113+
x-displayName: Test Service
114+
115+
x-tagGroups:
116+
- name: Test Group
117+
tags:
118+
- TestService

test/file_test_openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
openapi: 3.1.0
1+
openapi: 3.0.3
22

33
info:
44
description: test description

test/method_test_openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
openapi: 3.1.0
1+
openapi: 3.0.3
22

33
info:
44
description: test description

test/service_test_openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
openapi: 3.1.0
1+
openapi: 3.0.3
22

33
info:
44
description: test description

0 commit comments

Comments
 (0)