Skip to content

Commit

Permalink
Simplify code by centralizing import fmt mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Philzen committed May 28, 2024
1 parent f21bb55 commit 5b3ded9
Showing 1 changed file with 13 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,15 @@ public ModelsMap postProcessModels(ModelsMap objs) {

for (ModelMap m : objs.getModels()) {
CodegenModel model = m.getModel();
if (model.isEnum) {

if (model.isEnum
|| model.hasRequired
|| (model.oneOf != null && !model.oneOf.isEmpty())
|| (model.anyOf != null && !model.anyOf.isEmpty())) {
imports.add(createMapping("import", "fmt"));
}

if (model.isEnum) {
continue;
}

Expand All @@ -500,31 +507,11 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}
}

// additional import for different cases
boolean addedFmtImport = false;

// oneOf
if (model.oneOf != null && !model.oneOf.isEmpty()) {
imports.add(createMapping("import", "fmt"));
addedFmtImport = true;
}

// anyOf
if (model.anyOf != null && !model.anyOf.isEmpty()) {
imports.add(createMapping("import", "fmt"));
addedFmtImport = true;
}

if (model.hasRequired) {
if (!model.isAdditionalPropertiesTrue &&
(model.oneOf == null || model.oneOf.isEmpty()) &&
(model.anyOf == null || model.anyOf.isEmpty())) {
imports.add(createMapping("import", "bytes"));
}

if (!addedFmtImport) {
imports.add(createMapping("import", "fmt"));
}
if (model.hasRequired
&& !model.isAdditionalPropertiesTrue
&& (model.oneOf == null || model.oneOf.isEmpty())
&& (model.anyOf == null || model.anyOf.isEmpty())) {
imports.add(createMapping("import", "bytes"));
}

// additionalProperties: true and parent
Expand Down

0 comments on commit 5b3ded9

Please sign in to comment.