Skip to content

Commit bac320d

Browse files
Anthony TODISCOAnthony TODISCO
authored andcommitted
feat(protobuf-generator): Improve protobuf generation
* Improve management of inheritance * Improve management of discriminator * Allow to separate inline enums in external files * Add unit test
1 parent aae8ca1 commit bac320d

9 files changed

Lines changed: 1025 additions & 35 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenDiscriminator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ public MappedModel(String mappingName, String modelName, boolean explicitMapping
8888
this.explicitMapping = explicitMapping;
8989
}
9090

91+
public boolean isExplicitMapping() {
92+
return explicitMapping;
93+
}
94+
9195
public MappedModel(String mappingName, String modelName) {
9296
this(mappingName, modelName, false);
9397
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ProtobufSchemaCodegen.java

Lines changed: 606 additions & 17 deletions
Large diffs are not rendered by default.

modules/openapi-generator/src/main/resources/protobuf-schema/model.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import public "{{{.}}}.proto";
3737
{{#vendorExtensions.x-protobuf-type}}{{{.}}} {{/vendorExtensions.x-protobuf-type}}{{{vendorExtensions.x-protobuf-data-type}}} {{{name}}} = {{vendorExtensions.x-protobuf-index}}{{#vendorExtensions.x-protobuf-packed}} [packed=true]{{/vendorExtensions.x-protobuf-packed}}{{#vendorExtensions.x-protobuf-json-name}} [json_name="{{vendorExtensions.x-protobuf-json-name}}"]{{/vendorExtensions.x-protobuf-json-name}};
3838
{{/isEnum}}
3939
{{#isEnum}}
40+
{{^vendorExtensions.x-protobuf-enum-reference-import}}
4041
enum {{enumName}} {
4142
{{#allowableValues}}
4243
{{#enumVars}}
@@ -45,6 +46,7 @@ import public "{{{.}}}.proto";
4546
{{/allowableValues}}
4647
}
4748

49+
{{/vendorExtensions.x-protobuf-enum-reference-import}}
4850
{{enumName}} {{name}} = {{vendorExtensions.x-protobuf-index}};
4951
{{/isEnum}}
5052

modules/openapi-generator/src/test/java/org/openapitools/codegen/protobuf/ProtobufSchemaCodegenTest.java

Lines changed: 287 additions & 15 deletions
Large diffs are not rendered by default.

modules/openapi-generator/src/test/resources/3_0/allOf_composition_discriminator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ components:
8080
- $ref: '#/components/schemas/Lizard'
8181
discriminator:
8282
propertyName: petType
83-
# per https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#discriminatorObject
83+
# per https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#discriminator-object
8484
# this discriminator must be included to use it as a hint to pick a schema
8585
MyPetsNoDisc:
8686
oneOf:

modules/openapi-generator/src/test/resources/3_0/protobuf-schema/animal.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ message Animal {
1818

1919
string pet_type = 482112090;
2020

21-
string bark = 3016376;
22-
2321
string name = 3373707;
2422

2523
string fur_color = 478695002;
@@ -28,4 +26,6 @@ message Animal {
2826

2927
CareDetails care_details = 176721135;
3028

29+
string bark = 3016376;
30+
3131
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Enum extraction test
4+
version: '2.0'
5+
paths: {}
6+
components:
7+
schemas:
8+
SeparatedEnum:
9+
type: string
10+
enum:
11+
- VALUE1
12+
- VALUE2
13+
ModelWithEnums:
14+
properties:
15+
referenceEnumProperty:
16+
$ref: "#/components/schemas/SeparatedEnum"
17+
inlineEnumProperty:
18+
type: string
19+
enum:
20+
- VALUE2
21+
- VALUE3
22+
- VALUE4
23+
AllOfModelWithEnums:
24+
allOf:
25+
- $ref: "#/components/schemas/ModelWithEnums"
26+
- type: object
27+
properties:
28+
anotherInlineEnumProperty:
29+
type: string
30+
enum:
31+
- VALUE5
32+
- VALUE6
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Model Import Test
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
A:
9+
type: object
10+
description: Base model A that will be referenced multiple times
11+
properties:
12+
id:
13+
type: string
14+
value:
15+
type: string
16+
required:
17+
- id
18+
19+
SubModel:
20+
type: object
21+
description: SubModel that uses model A
22+
allOf:
23+
- $ref: '#/components/schemas/ExtensibleModel'
24+
- type: object
25+
properties:
26+
subField:
27+
type: string
28+
aReference:
29+
$ref: '#/components/schemas/A'
30+
required:
31+
- subField
32+
33+
Model:
34+
type: object
35+
description: Main model that uses A directly and via SubModel
36+
properties:
37+
name:
38+
type: string
39+
directA:
40+
$ref: '#/components/schemas/A'
41+
nestedSubModel:
42+
$ref: '#/components/schemas/SubModel'
43+
required:
44+
- name
45+
46+
ExtensibleModel:
47+
type: object
48+
description: Extensible model with discriminator that uses model A
49+
discriminator:
50+
propertyName: type
51+
mapping:
52+
child1: '#/components/schemas/ChildModel_1'
53+
child2: '#/components/schemas/ChildModel_2'
54+
properties:
55+
type:
56+
type: string
57+
extensibleA:
58+
$ref: '#/components/schemas/A'
59+
required:
60+
- type
61+
62+
ChildModel_1:
63+
allOf:
64+
- $ref: '#/components/schemas/ExtensibleModel'
65+
- type: object
66+
properties:
67+
childSpecificField:
68+
type: string
69+
childA:
70+
$ref: '#/components/schemas/A'
71+
ChildModel_2:
72+
allOf:
73+
- $ref: '#/components/schemas/SubExtensibleModel'
74+
- type: object
75+
properties:
76+
anotherChildSpecificField:
77+
type: integer
78+
directA:
79+
$ref: '#/components/schemas/A'
80+
81+
SubExtensibleModel:
82+
allOf:
83+
- $ref: '#/components/schemas/ExtensibleModel'
84+
- type: object
85+
properties:
86+
aSubExtensibleField:
87+
type: boolean
88+
subDirectA:
89+
$ref: '#/components/schemas/A'

modules/openapi-generator/src/test/resources/3_0/protobuf-schema/pet.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ message Pet {
2626

2727
bool loves_rocks = 465093427;
2828

29+
bool has_legs = 140596906;
30+
2931
}

0 commit comments

Comments
 (0)