Skip to content

Commit fa4f7e0

Browse files
authored
[GO] fix: stops adding imports for nested structs (#13833)
1 parent 06096d7 commit fa4f7e0

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,39 @@ public ModelsMap postProcessModels(ModelsMap objs) {
627627
iterator.remove();
628628
}
629629

630-
boolean addedTimeImport = false;
631-
boolean addedOSImport = false;
632630
for (ModelMap m : objs.getModels()) {
631+
boolean addedTimeImport = false;
632+
boolean addedOSImport = false;
633633
CodegenModel model = m.getModel();
634-
for (CodegenProperty cp : model.vars) {
634+
635+
List<CodegenProperty> inheritedProperties = new ArrayList<>();
636+
if (model.getComposedSchemas() != null) {
637+
if (model.getComposedSchemas().getAllOf() != null) {
638+
inheritedProperties.addAll(model.getComposedSchemas().getAllOf());
639+
}
640+
if (model.getComposedSchemas().getAnyOf() != null) {
641+
inheritedProperties.addAll(model.getComposedSchemas().getAnyOf());
642+
}
643+
if (model.getComposedSchemas().getOneOf() != null) {
644+
inheritedProperties.addAll(model.getComposedSchemas().getOneOf());
645+
}
646+
}
647+
648+
List<CodegenProperty> codegenProperties = new ArrayList<>();
649+
if(model.getIsModel() || model.getComposedSchemas() == null) {
650+
// If the model is a model, use model.vars as it only
651+
// contains properties the generated struct will own itself.
652+
// If model is no model and it has no composed schemas use
653+
// model.vars.
654+
codegenProperties.addAll(model.vars);
655+
} else {
656+
// If the model is no model, but is a
657+
// allOf, anyOf or oneOf, add all first level options
658+
// from allOf, anyOf or oneOf.
659+
codegenProperties.addAll(inheritedProperties);
660+
}
661+
662+
for (CodegenProperty cp : codegenProperties) {
635663
if (!addedTimeImport && ("time.Time".equals(cp.dataType) ||
636664
(cp.items != null && "time.Time".equals(cp.items.dataType)))) {
637665
imports.add(createMapping("import", "time"));
@@ -651,7 +679,6 @@ public ModelsMap postProcessModels(ModelsMap objs) {
651679
// if oneOf contains "time.Time" type
652680
if (!addedTimeImport && model.oneOf != null && model.oneOf.contains("time.Time")) {
653681
imports.add(createMapping("import", "time"));
654-
addedTimeImport = true;
655682
}
656683

657684
// if oneOf contains "null" type

0 commit comments

Comments
 (0)