Skip to content

Commit 3a96905

Browse files
author
Josh
committed
Fix security without scopes
1 parent 682f8fb commit 3a96905

File tree

6 files changed

+61
-72
lines changed

6 files changed

+61
-72
lines changed

internal/generator/path.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func (g *Generator) addOperation(p addOperationParams) error {
346346
}
347347

348348
if len(methodOptions.Security) > 0 {
349-
// Use service defined security.
349+
// Use method defined security.
350350
for _, s := range methodOptions.Security {
351351
// If one is empty, this is how we'll clear it on an override.
352352
// e.g. security: {}
@@ -379,9 +379,15 @@ func (g *Generator) addOperation(p addOperationParams) error {
379379
if len(p.security) > 0 {
380380
op.Security = new(openapi3.SecurityRequirements)
381381
for _, sec := range p.security {
382-
op.Security = op.Security.With(openapi3.SecurityRequirement{
383-
sec.Name: sec.Scopes,
384-
})
382+
req := openapi3.SecurityRequirement{
383+
sec.Name: make([]string, 0),
384+
}
385+
386+
if len(sec.Scopes) > 0 {
387+
req[sec.Name] = sec.Scopes
388+
}
389+
390+
op.Security = op.Security.With(req)
385391
}
386392
}
387393

test/field_test.proto

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ syntax = "proto3";
22

33
package test.api;
44

5+
import "google/protobuf/timestamp.proto";
6+
import "oapi/v1/field.proto";
57
import "oapi/v1/file.proto";
6-
import "oapi/v1/service.proto";
78
import "oapi/v1/method.proto";
8-
import "oapi/v1/field.proto";
9-
import "google/protobuf/timestamp.proto";
9+
import "oapi/v1/service.proto";
1010

1111
option go_package = "github.com/technicallyjosh/protoc-gen-openapi/test_api";
12-
1312
option (oapi.v1.file) = {
1413
host: "swagger.io"
1514
prefix: "/v1"
@@ -23,15 +22,11 @@ service TestService {
2322
};
2423

2524
rpc TestFieldTypes(TestFieldTypesRequest) returns (TestFieldTypesResponse) {
26-
option (oapi.v1.method) = {
27-
post: "TestFieldTypes"
28-
};
29-
};
25+
option (oapi.v1.method) = {post: "TestFieldTypes"};
26+
}
3027

3128
rpc TestFieldExamples(TestFieldExamplesRequest) returns (TestFieldExamplesResponse) {
32-
option (oapi.v1.method) = {
33-
post: "TestFieldExamples"
34-
};
29+
option (oapi.v1.method) = {post: "TestFieldExamples"};
3530
}
3631
}
3732

@@ -57,12 +52,10 @@ message TestFieldTypesRequest {
5752
repeated string repeated_string = 7;
5853
repeated Message repeated_message = 8;
5954
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-
];
55+
google.protobuf.Timestamp message_at = 10 [(oapi.v1.options) = {
56+
as_type: "string"
57+
format: "date-time"
58+
}];
6659
}
6760

6861
message TestFieldTypesResponse {}

test/file_test.proto

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ package test.api;
55
import "oapi/v1/file.proto";
66

77
option go_package = "github.com/technicallyjosh/protoc-gen-openapi/test_api";
8-
98
option (oapi.v1.file) = {
109
host: "swagger.io"
1110

1211
servers: [
13-
{
14-
url: "swagger.one"
15-
},
16-
{
17-
url: "swagger.two"
18-
}
19-
]
12+
{url: "swagger.one"},
13+
{url: "swagger.two"}]
2014

2115
security_schemes: {
2216
name: "bearer_auth"

test/method_test.proto

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@ syntax = "proto3";
33
package test.api;
44

55
import "oapi/v1/file.proto";
6-
import "oapi/v1/service.proto";
76
import "oapi/v1/method.proto";
7+
import "oapi/v1/service.proto";
88

99
option go_package = "github.com/technicallyjosh/protoc-gen-openapi/test_api";
10-
1110
option (oapi.v1.file) = {
12-
servers {
13-
url: "swagger.io"
14-
}
11+
servers {url: "swagger.io"}
1512
prefix: "/v1"
13+
security_schemes: {
14+
name: "bearer_auth"
15+
scheme: {
16+
type: "http"
17+
scheme: "bearer"
18+
bearer_format: "JWT"
19+
}
20+
}
1621
};
1722

1823
service TestService {
1924
option (oapi.v1.service) = {
20-
servers {
21-
url: "api.swagger.io"
22-
}
25+
servers {url: "api.swagger.io"}
2326
x_display_name: "Test Service"
2427
x_tag_group: "Test Group"
2528
};
2629

2730
rpc TestEmptyPost(TestEmptyPostRequest) returns (TestEmptyPostResponse) {
2831
option (oapi.v1.method) = {
2932
post: "TestEmptyPost"
30-
servers {
31-
url: "test.swagger.io"
32-
}
33-
add_servers: {
34-
url: "test.added.io"
35-
}
33+
servers {url: "test.swagger.io"}
34+
add_servers: {url: "test.added.io"}
35+
security: {name: "bearer_auth"}
3636
};
37-
};
37+
}
3838
}
3939

4040
message TestEmptyPostRequest {}

test/method_test_openapi.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ paths:
2323
description: ''
2424
default:
2525
$ref: '#/components/responses/default'
26+
security:
27+
- bearer_auth: [ ]
2628
servers:
2729
- url: https://test.swagger.io
2830
- url: https://test.added.io
@@ -37,6 +39,11 @@ components:
3739
schema:
3840
$ref: '#/components/schemas/test.api.Error'
3941
description: ""
42+
securitySchemes:
43+
bearer_auth:
44+
type: http
45+
scheme: bearer
46+
bearerFormat: JWT
4047
schemas:
4148
test.api.Error:
4249
properties:

test/service_test.proto

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ syntax = "proto3";
33
package test.api;
44

55
import "oapi/v1/file.proto";
6-
import "oapi/v1/service.proto";
76
import "oapi/v1/method.proto";
7+
import "oapi/v1/service.proto";
88

99
option go_package = "github.com/technicallyjosh/protoc-gen-openapi/test_api";
10-
1110
option (oapi.v1.file) = {
12-
servers {
13-
url: "swagger.io"
14-
}
11+
servers {url: "swagger.io"}
1512
prefix: "/v1"
1613

1714
security_schemes: {
@@ -25,27 +22,24 @@ option (oapi.v1.file) = {
2522

2623
security: {
2724
name: "bearer_auth"
28-
scopes: ["scope:1", "scope:2"]
25+
scopes: [
26+
"scope:1",
27+
"scope:2"
28+
]
2929
}
3030
};
3131

3232
service TestService {
3333
option (oapi.v1.service) = {
34-
servers {
35-
url: "api.swagger.io"
36-
}
37-
add_servers {
38-
url: "api.added.io"
39-
}
34+
servers {url: "api.swagger.io"}
35+
add_servers {url: "api.added.io"}
4036
x_display_name: "Test Service"
4137
x_tag_group: "Test Group"
4238
};
4339

4440
rpc TestGet(TestGetRequest) returns (TestGetResponse) {
45-
option (oapi.v1.method) = {
46-
get: "TestGet"
47-
};
48-
};
41+
option (oapi.v1.method) = {get: "TestGet"};
42+
}
4943

5044
rpc TestPost(TestPostRequest) returns (TestPostResponse) {
5145
option (oapi.v1.method) = {
@@ -57,34 +51,29 @@ service TestService {
5751

5852
service Test2Service {
5953
option (oapi.v1.service) = {
60-
add_servers {
61-
url: "api.added.io"
62-
}
54+
add_servers {url: "api.added.io"}
6355
x_display_name: "Test 2 Service"
6456
x_tag_group: "Test 2 Group"
6557
};
6658

6759
rpc TestGet(TestGetRequest) returns (TestGetResponse) {
68-
option (oapi.v1.method) = {
69-
get: "Test2Get"
70-
};
71-
};
60+
option (oapi.v1.method) = {get: "Test2Get"};
61+
}
7262
}
7363

74-
message TestGetRequest{
64+
message TestGetRequest {
7565
string value = 1;
7666
}
7767

78-
message TestGetResponse{
68+
message TestGetResponse {
7969
string value = 1;
8070
}
8171

82-
message TestPostRequest{}
72+
message TestPostRequest {}
8373

84-
message TestPostResponse{}
74+
message TestPostResponse {}
8575

8676
message Error {
8777
string code = 1;
8878
string msg = 2;
8979
}
90-

0 commit comments

Comments
 (0)