[GO] fix: stops adding imports for nested structs (#13833)

This commit is contained in:
Nick Ufer 2022-10-31 15:14:42 +01:00 committed by GitHub
parent 06096d7f77
commit fa4f7e07fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -627,11 +627,39 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
iterator.remove();
}
boolean addedTimeImport = false;
boolean addedOSImport = false;
for (ModelMap m : objs.getModels()) {
boolean addedTimeImport = false;
boolean addedOSImport = false;
CodegenModel model = m.getModel();
for (CodegenProperty cp : model.vars) {
List<CodegenProperty> inheritedProperties = new ArrayList<>();
if (model.getComposedSchemas() != null) {
if (model.getComposedSchemas().getAllOf() != null) {
inheritedProperties.addAll(model.getComposedSchemas().getAllOf());
}
if (model.getComposedSchemas().getAnyOf() != null) {
inheritedProperties.addAll(model.getComposedSchemas().getAnyOf());
}
if (model.getComposedSchemas().getOneOf() != null) {
inheritedProperties.addAll(model.getComposedSchemas().getOneOf());
}
}
List<CodegenProperty> codegenProperties = new ArrayList<>();
if(model.getIsModel() || model.getComposedSchemas() == null) {
// If the model is a model, use model.vars as it only
// contains properties the generated struct will own itself.
// If model is no model and it has no composed schemas use
// model.vars.
codegenProperties.addAll(model.vars);
} else {
// If the model is no model, but is a
// allOf, anyOf or oneOf, add all first level options
// from allOf, anyOf or oneOf.
codegenProperties.addAll(inheritedProperties);
}
for (CodegenProperty cp : codegenProperties) {
if (!addedTimeImport && ("time.Time".equals(cp.dataType) ||
(cp.items != null && "time.Time".equals(cp.items.dataType)))) {
imports.add(createMapping("import", "time"));
@ -651,7 +679,6 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
// if oneOf contains "time.Time" type
if (!addedTimeImport && model.oneOf != null && model.oneOf.contains("time.Time")) {
imports.add(createMapping("import", "time"));
addedTimeImport = true;
}
// if oneOf contains "null" type