forked from loafle/openapi-generator-original
beter code format for protobuf java files (#7857)
This commit is contained in:
parent
4f2f80766e
commit
ca6fcaf92a
@ -42,10 +42,10 @@ import static org.openapitools.codegen.utils.StringUtils.camelize;
|
|||||||
import static org.openapitools.codegen.utils.StringUtils.underscore;
|
import static org.openapitools.codegen.utils.StringUtils.underscore;
|
||||||
|
|
||||||
public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConfig {
|
public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConfig {
|
||||||
|
|
||||||
private static final String IMPORT = "import";
|
private static final String IMPORT = "import";
|
||||||
|
|
||||||
private static final String IMPORTS = "imports";
|
private static final String IMPORTS = "imports";
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
|
||||||
|
|
||||||
@ -86,20 +86,6 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
modelPackage = "messages";
|
modelPackage = "messages";
|
||||||
apiPackage = "services";
|
apiPackage = "services";
|
||||||
|
|
||||||
/*setReservedWordsLowerCase(
|
|
||||||
Arrays.asList(
|
|
||||||
// data type
|
|
||||||
"nil", "string", "boolean", "number", "userdata", "thread",
|
|
||||||
"table",
|
|
||||||
|
|
||||||
// reserved words: http://www.lua.org/manual/5.1/manual.html#2.1
|
|
||||||
"and", "break", "do", "else", "elseif",
|
|
||||||
"end", "false", "for", "function", "if",
|
|
||||||
"in", "local", "nil", "not", "or",
|
|
||||||
"repeat", "return", "then", "true", "until", "while"
|
|
||||||
)
|
|
||||||
);*/
|
|
||||||
|
|
||||||
defaultIncludes = new HashSet<String>(
|
defaultIncludes = new HashSet<String>(
|
||||||
Arrays.asList(
|
Arrays.asList(
|
||||||
"map",
|
"map",
|
||||||
@ -129,7 +115,6 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
|
|
||||||
instantiationTypes.clear();
|
instantiationTypes.clear();
|
||||||
instantiationTypes.put("array", "repeat");
|
instantiationTypes.put("array", "repeat");
|
||||||
//instantiationTypes.put("map", "map");
|
|
||||||
|
|
||||||
// ref: https://developers.google.com/protocol-buffers/docs/proto
|
// ref: https://developers.google.com/protocol-buffers/docs/proto
|
||||||
typeMapping.clear();
|
typeMapping.clear();
|
||||||
@ -153,24 +138,12 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
typeMapping.put("ByteArray", "bytes");
|
typeMapping.put("ByteArray", "bytes");
|
||||||
typeMapping.put("object", "TODO_OBJECT_MAPPING");
|
typeMapping.put("object", "TODO_OBJECT_MAPPING");
|
||||||
|
|
||||||
importMapping.clear();
|
importMapping.clear();
|
||||||
/*
|
|
||||||
importMapping = new HashMap<String, String>();
|
|
||||||
importMapping.put("time.Time", "time");
|
|
||||||
importMapping.put("*os.File", "os");
|
|
||||||
importMapping.put("os", "io/ioutil");
|
|
||||||
*/
|
|
||||||
|
|
||||||
modelDocTemplateFiles.put("model_doc.mustache", ".md");
|
modelDocTemplateFiles.put("model_doc.mustache", ".md");
|
||||||
apiDocTemplateFiles.put("api_doc.mustache", ".md");
|
apiDocTemplateFiles.put("api_doc.mustache", ".md");
|
||||||
|
|
||||||
cliOptions.clear();
|
cliOptions.clear();
|
||||||
/*cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "GraphQL package name (convention: lowercase).")
|
|
||||||
.defaultValue("openapi2graphql"));
|
|
||||||
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "GraphQL package version.")
|
|
||||||
.defaultValue("1.0.0"));
|
|
||||||
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC)
|
|
||||||
.defaultValue(Boolean.TRUE.toString()));*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,10 +165,6 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
}
|
}
|
||||||
|
|
||||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||||
//supportingFiles.add(new SupportingFile("root.mustache", "", packageName + ".proto"));
|
|
||||||
//supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
|
||||||
//supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"))
|
|
||||||
//supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -259,59 +228,59 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
}
|
}
|
||||||
return objs;
|
return objs;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
|
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
|
||||||
super.postProcessAllModels(objs);
|
super.postProcessAllModels(objs);
|
||||||
|
|
||||||
Map<String, CodegenModel> allModels = this.getAllModels(objs);
|
Map<String, CodegenModel> allModels = this.getAllModels(objs);
|
||||||
|
|
||||||
for (CodegenModel cm : allModels.values()) {
|
for (CodegenModel cm : allModels.values()) {
|
||||||
// Replicate all attributes from children to parents in case of allof, as there is no inheritance
|
// Replicate all attributes from children to parents in case of allof, as there is no inheritance
|
||||||
if (!cm.allOf.isEmpty() && cm.getParentModel() != null) {
|
if (!cm.allOf.isEmpty() && cm.getParentModel() != null) {
|
||||||
CodegenModel parentCM = cm.getParentModel();
|
CodegenModel parentCM = cm.getParentModel();
|
||||||
for (CodegenProperty var : cm.getVars()) {
|
for (CodegenProperty var : cm.getVars()) {
|
||||||
if (!parentVarsContainsVar(parentCM.vars, var)) {
|
if (!parentVarsContainsVar(parentCM.vars, var)) {
|
||||||
parentCM.vars.add(var);
|
parentCM.vars.add(var);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
// add all imports from child
|
||||||
// add all imports from child
|
cm.getImports().stream()
|
||||||
cm.getImports().stream()
|
// Filter self import && child import
|
||||||
// Filter self import && child import
|
.filter(importFromList -> !parentCM.getClassname().equalsIgnoreCase(importFromList) && !cm.getClassname().equalsIgnoreCase(importFromList))
|
||||||
.filter(importFromList -> !parentCM.getClassname().equalsIgnoreCase(importFromList) && !cm.getClassname().equalsIgnoreCase(importFromList))
|
.forEach(importFromList -> this.addImport(objs, parentCM, importFromList));
|
||||||
.forEach(importFromList -> this.addImport(objs, parentCM, importFromList));
|
}
|
||||||
}
|
}
|
||||||
}
|
return objs;
|
||||||
return objs;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addImport(Map<String, Object> objs, CodegenModel cm, String importValue) {
|
|
||||||
String modelFileName = this.toModelFilename(importValue);
|
|
||||||
boolean skipImport = isImportAlreadyPresentInModel(objs, cm, modelFileName);
|
|
||||||
if (!skipImport) {
|
|
||||||
this.addImport(cm, importValue);
|
|
||||||
Map<String, Object> importItem = new HashMap<>();
|
|
||||||
importItem.put(IMPORT, modelFileName);
|
|
||||||
((List<Map<String, Object>>)((Map<String, Object>)objs.get(cm.getName())).get(IMPORTS)).add(importItem);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isImportAlreadyPresentInModel(Map<String, Object> objs, CodegenModel cm, String importValue) {
|
public void addImport(Map<String, Object> objs, CodegenModel cm, String importValue) {
|
||||||
boolean skipImport = false;
|
String modelFileName = this.toModelFilename(importValue);
|
||||||
List<Map<String, Object>> cmImports = ((List<Map<String, Object>>)((Map<String, Object>)objs.get(cm.getName())).get(IMPORTS));
|
boolean skipImport = isImportAlreadyPresentInModel(objs, cm, modelFileName);
|
||||||
for (Map<String, Object> cmImportItem: cmImports) {
|
if (!skipImport) {
|
||||||
for (Entry<String, Object> cmImportItemEntry : cmImportItem.entrySet()) {
|
this.addImport(cm, importValue);
|
||||||
if (importValue.equals(cmImportItemEntry.getValue())) {
|
Map<String, Object> importItem = new HashMap<>();
|
||||||
skipImport = true;
|
importItem.put(IMPORT, modelFileName);
|
||||||
break;
|
((List<Map<String, Object>>) ((Map<String, Object>) objs.get(cm.getName())).get(IMPORTS)).add(importItem);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return skipImport;
|
|
||||||
}
|
private boolean isImportAlreadyPresentInModel(Map<String, Object> objs, CodegenModel cm, String importValue) {
|
||||||
|
boolean skipImport = false;
|
||||||
|
List<Map<String, Object>> cmImports = ((List<Map<String, Object>>) ((Map<String, Object>) objs.get(cm.getName())).get(IMPORTS));
|
||||||
|
for (Map<String, Object> cmImportItem : cmImports) {
|
||||||
|
for (Entry<String, Object> cmImportItemEntry : cmImportItem.entrySet()) {
|
||||||
|
if (importValue.equals(cmImportItemEntry.getValue())) {
|
||||||
|
skipImport = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return skipImport;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String escapeUnsafeCharacters(String input) {
|
public String escapeUnsafeCharacters(String input) {
|
||||||
@ -525,32 +494,33 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
}
|
}
|
||||||
return super.getTypeDeclaration(p);
|
return super.getTypeDeclaration(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int generateFieldNumberFromString(String name) throws ProtoBufIndexComputationException {
|
private int generateFieldNumberFromString(String name) throws ProtoBufIndexComputationException {
|
||||||
// Max value from developers.google.com/protocol-buffers/docs/proto3#assigning_field_numbers
|
// Max value from developers.google.com/protocol-buffers/docs/proto3#assigning_field_numbers
|
||||||
int fieldNumber = Math.abs(name.hashCode()%536870911);
|
int fieldNumber = Math.abs(name.hashCode() % 536870911);
|
||||||
if (19000 <= fieldNumber && fieldNumber <= 19999 ) {
|
if (19000 <= fieldNumber && fieldNumber <= 19999) {
|
||||||
LOGGER.error("Generated field number is in reserved range (19000, 19999) for %s, %d", name, fieldNumber);
|
LOGGER.error("Generated field number is in reserved range (19000, 19999) for %s, %d", name, fieldNumber);
|
||||||
throw new ProtoBufIndexComputationException("Generated field number is in reserved range (19000, 19999).");
|
throw new ProtoBufIndexComputationException("Generated field number is in reserved range (19000, 19999).");
|
||||||
}
|
}
|
||||||
return fieldNumber;
|
return fieldNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the var provided is already in the list of the parent's vars, matching the type and the name
|
* Checks if the var provided is already in the list of the parent's vars, matching the type and the name
|
||||||
|
*
|
||||||
* @param parentVars list of parent's vars
|
* @param parentVars list of parent's vars
|
||||||
* @param var var to compare
|
* @param var var to compare
|
||||||
* @return true if the var is already in the parent's list, false otherwise
|
* @return true if the var is already in the parent's list, false otherwise
|
||||||
*/
|
*/
|
||||||
private boolean parentVarsContainsVar(List<CodegenProperty> parentVars, CodegenProperty var) {
|
private boolean parentVarsContainsVar(List<CodegenProperty> parentVars, CodegenProperty var) {
|
||||||
boolean containsVar = false;
|
boolean containsVar = false;
|
||||||
for (CodegenProperty parentVar : parentVars) {
|
for (CodegenProperty parentVar : parentVars) {
|
||||||
if (var.getDataType().equals(parentVar.getDataType())
|
if (var.getDataType().equals(parentVar.getDataType())
|
||||||
&& var.getName().equals(parentVar.getName())) {
|
&& var.getName().equals(parentVar.getName())) {
|
||||||
containsVar = true;
|
containsVar = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return containsVar;
|
||||||
}
|
}
|
||||||
return containsVar;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -46,10 +46,10 @@ public class ProtobufSchemaCodegenTest {
|
|||||||
Assert.assertTrue(featureSet.getWireFormatFeatures().contains(WireFormatFeature.PROTOBUF));
|
Assert.assertTrue(featureSet.getWireFormatFeatures().contains(WireFormatFeature.PROTOBUF));
|
||||||
Assert.assertEquals(featureSet.getWireFormatFeatures().size(), 1);
|
Assert.assertEquals(featureSet.getWireFormatFeatures().size(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCodeGenWithAllOf() throws IOException {
|
public void testCodeGenWithAllOf() throws IOException {
|
||||||
File output = Files.createTempDirectory("test").toFile();
|
File output = Files.createTempDirectory("test").toFile();
|
||||||
|
|
||||||
final CodegenConfigurator configurator = new CodegenConfigurator()
|
final CodegenConfigurator configurator = new CodegenConfigurator()
|
||||||
.setGeneratorName("protobuf-schema")
|
.setGeneratorName("protobuf-schema")
|
||||||
@ -68,10 +68,10 @@ public class ProtobufSchemaCodegenTest {
|
|||||||
output.delete();
|
output.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertFileEquals(Path generatedFilePath, Path expectedFilePath) throws IOException {
|
private void assertFileEquals(Path generatedFilePath, Path expectedFilePath) throws IOException {
|
||||||
String generatedFile = new String(Files.readAllBytes(generatedFilePath), StandardCharsets.UTF_8);
|
String generatedFile = new String(Files.readAllBytes(generatedFilePath), StandardCharsets.UTF_8);
|
||||||
String expectedFile = new String(Files.readAllBytes(expectedFilePath), StandardCharsets.UTF_8);
|
String expectedFile = new String(Files.readAllBytes(expectedFilePath), StandardCharsets.UTF_8);
|
||||||
|
|
||||||
assertEquals(generatedFile, expectedFile);
|
assertEquals(generatedFile, expectedFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,10 +15,10 @@ package petstore;
|
|||||||
|
|
||||||
message ApiResponse {
|
message ApiResponse {
|
||||||
|
|
||||||
int32 code = 1;
|
int32 code = 3059181;
|
||||||
|
|
||||||
string type = 2;
|
string type = 3575610;
|
||||||
|
|
||||||
string message = 3;
|
string message = 418054152;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,8 @@ package petstore;
|
|||||||
|
|
||||||
message Category {
|
message Category {
|
||||||
|
|
||||||
int64 id = 1;
|
int64 id = 3355;
|
||||||
|
|
||||||
string name = 2;
|
string name = 3373707;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,9 @@ package petstore;
|
|||||||
message InlineObject {
|
message InlineObject {
|
||||||
|
|
||||||
// Updated name of the pet
|
// Updated name of the pet
|
||||||
string name = 1;
|
string name = 3373707;
|
||||||
|
|
||||||
// Updated status of the pet
|
// Updated status of the pet
|
||||||
string status = 2;
|
string status = 355610639;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,9 @@ package petstore;
|
|||||||
message InlineObject1 {
|
message InlineObject1 {
|
||||||
|
|
||||||
// Additional data to pass to server
|
// Additional data to pass to server
|
||||||
string additionalMetadata = 1;
|
string additionalMetadata = 400408697;
|
||||||
|
|
||||||
// file to upload
|
// file to upload
|
||||||
string file = 2;
|
string file = 3143036;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,13 +15,13 @@ package petstore;
|
|||||||
|
|
||||||
message Order {
|
message Order {
|
||||||
|
|
||||||
int64 id = 1;
|
int64 id = 3355;
|
||||||
|
|
||||||
int64 petId = 2;
|
int64 petId = 106557082;
|
||||||
|
|
||||||
int32 quantity = 3;
|
int32 quantity = 211262327;
|
||||||
|
|
||||||
string shipDate = 4;
|
string shipDate = 517554166;
|
||||||
|
|
||||||
// Order Status
|
// Order Status
|
||||||
enum StatusEnum {
|
enum StatusEnum {
|
||||||
@ -30,8 +30,8 @@ message Order {
|
|||||||
DELIVERED = 2;
|
DELIVERED = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
StatusEnum status = 5;
|
StatusEnum status = 355610639;
|
||||||
|
|
||||||
bool complete = 6;
|
bool complete = 62574280;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,15 +17,15 @@ import public "models/tag.proto";
|
|||||||
|
|
||||||
message Pet {
|
message Pet {
|
||||||
|
|
||||||
int64 id = 1;
|
int64 id = 3355;
|
||||||
|
|
||||||
Category category = 2;
|
Category category = 50511102;
|
||||||
|
|
||||||
string name = 3;
|
string name = 3373707;
|
||||||
|
|
||||||
repeated string photoUrls = 4;
|
repeated string photoUrls = 311086539;
|
||||||
|
|
||||||
repeated Tag tags = 5;
|
repeated Tag tags = 3552281;
|
||||||
|
|
||||||
// pet status in the store
|
// pet status in the store
|
||||||
enum StatusEnum {
|
enum StatusEnum {
|
||||||
@ -34,6 +34,6 @@ message Pet {
|
|||||||
SOLD = 2;
|
SOLD = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
StatusEnum status = 6;
|
StatusEnum status = 355610639;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,8 @@ package petstore;
|
|||||||
|
|
||||||
message Tag {
|
message Tag {
|
||||||
|
|
||||||
int64 id = 1;
|
int64 id = 3355;
|
||||||
|
|
||||||
string name = 2;
|
string name = 3373707;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,21 +15,21 @@ package petstore;
|
|||||||
|
|
||||||
message User {
|
message User {
|
||||||
|
|
||||||
int64 id = 1;
|
int64 id = 3355;
|
||||||
|
|
||||||
string username = 2;
|
string username = 265713450;
|
||||||
|
|
||||||
string firstName = 3;
|
string firstName = 132835675;
|
||||||
|
|
||||||
string lastName = 4;
|
string lastName = 385857985;
|
||||||
|
|
||||||
string email = 5;
|
string email = 96619420;
|
||||||
|
|
||||||
string password = 6;
|
string password = 143243933;
|
||||||
|
|
||||||
string phone = 7;
|
string phone = 106642798;
|
||||||
|
|
||||||
// User Status
|
// User Status
|
||||||
int32 userStatus = 8;
|
int32 userStatus = 517890975;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user