mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-01 21:20:55 +00:00
[Protobuf-Schema] Add enum as model support (#10868)
* [Protobuf-Schema] Add enum as model support * [Protobuf-Schema] Bugfix: Reverted classname modification * [Protobuf-Scheme] Mustache newlines update * [Protobuf-Schema] Renamed addModelEnumIndexes to addEnumIndexes * [Protobuf-Schema] Add enum as model support * [Protobuf-Schema] Bugfix: Reverted classname modification * [Protobuf-Scheme] Mustache newlines update * [Protobuf-Schema] Renamed addModelEnumIndexes to addEnumIndexes Co-authored-by: Tomáš Čermák <cermak@merica.cz>
This commit is contained in:
parent
6779c33b9d
commit
fdea71b26e
@ -183,6 +183,14 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
||||
return camelize(sanitizeName(operationId));
|
||||
}
|
||||
|
||||
public void addEnumIndexes(List<Map<String, Object>> enumVars){
|
||||
int enumIndex = 0;
|
||||
for (Map<String, Object> enumVar : enumVars) {
|
||||
enumVar.put("protobuf-enum-index", enumIndex);
|
||||
enumIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||
objs = postProcessModelsEnum(objs);
|
||||
@ -192,6 +200,14 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
||||
Map<String, Object> mo = (Map<String, Object>) _mo;
|
||||
CodegenModel cm = (CodegenModel) mo.get("model");
|
||||
|
||||
if(cm.isEnum) {
|
||||
Map<String, Object> allowableValues = cm.getAllowableValues();
|
||||
if (allowableValues.containsKey("enumVars")) {
|
||||
List<Map<String, Object>> enumVars = (List<Map<String, Object>>)allowableValues.get("enumVars");
|
||||
addEnumIndexes(enumVars);
|
||||
}
|
||||
}
|
||||
|
||||
for (CodegenProperty var : cm.vars) {
|
||||
// add x-protobuf-type: repeated if it's an array
|
||||
if (Boolean.TRUE.equals(var.isArray)) {
|
||||
@ -209,12 +225,8 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
||||
}
|
||||
|
||||
if (var.isEnum && var.allowableValues.containsKey("enumVars")) {
|
||||
List<Map<String, Object>> enumVars = (List<Map<String, Object>>) var.allowableValues.get("enumVars");
|
||||
int enumIndex = 0;
|
||||
for (Map<String, Object> enumVar : enumVars) {
|
||||
enumVar.put("protobuf-enum-index", enumIndex);
|
||||
enumIndex++;
|
||||
}
|
||||
List<Map<String, Object>> enumVars = (List<Map<String, Object>>)var.allowableValues.get("enumVars");
|
||||
addEnumIndexes(enumVars);
|
||||
}
|
||||
|
||||
// Add x-protobuf-index, unless already specified
|
||||
|
7
modules/openapi-generator/src/main/resources/protobuf-schema/enum.mustache
vendored
Normal file
7
modules/openapi-generator/src/main/resources/protobuf-schema/enum.mustache
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
enum {{classname}} {
|
||||
{{#allowableValues}}
|
||||
{{#enumVars}}
|
||||
{{{name}}} = {{{protobuf-enum-index}}};
|
||||
{{/enumVars}}
|
||||
{{/allowableValues}}
|
||||
}
|
@ -11,7 +11,7 @@ import public "{{{modelPackage}}}/{{{import}}}.proto";
|
||||
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
message {{classname}} {
|
||||
{{#isEnum}}{{>enum}}{{/isEnum}}{{^isEnum}}message {{classname}} {
|
||||
|
||||
{{#vars}}
|
||||
{{#description}}
|
||||
@ -34,5 +34,6 @@ message {{classname}} {
|
||||
|
||||
{{/vars}}
|
||||
}
|
||||
{{/isEnum}}
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user