Skip to content

Commit 8f8001e

Browse files
authored
fix(go): do not generate unused imports (#22524)
Fixes #20599
1 parent 5c28ab4 commit 8f8001e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
520520
addedFmtImport = true;
521521
}
522522

523-
if (model.hasRequired) {
523+
if (generateUnmarshalJSON && model.hasRequired) {
524524
if (!model.isAdditionalPropertiesTrue &&
525525
(model.oneOf == null || model.oneOf.isEmpty()) &&
526526
(model.anyOf == null || model.anyOf.isEmpty())) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,27 @@ public void testVendorExtensionSkipGenerateUnmarshalJson() throws IOException {
354354
"func (o *BaseItem) UnmarshalJSON(data []byte) (err error) {");
355355
}
356356

357+
@Test
358+
public void testNoImportsWithoutUnmarshal() throws IOException {
359+
Map<String, Object> properties = new HashMap<>();
360+
properties.put(CodegenConstants.GENERATE_UNMARSHAL_JSON, false);
361+
362+
File output = Files.createTempDirectory("test").toFile();
363+
output.deleteOnExit();
364+
365+
final CodegenConfigurator configurator = new CodegenConfigurator()
366+
.setGeneratorName("go")
367+
.setAdditionalProperties(properties)
368+
.setInputSpec("src/test/resources/3_0/petstore.yaml")
369+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
370+
371+
DefaultGenerator generator = new DefaultGenerator();
372+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
373+
files.forEach(File::deleteOnExit);
374+
375+
TestUtils.assertFileNotContains(Paths.get(output + "/model_pet.go"), "bytes");
376+
}
377+
357378
@Test
358379
public void testAdditionalPropertiesWithGoMod() throws Exception {
359380
File output = Files.createTempDirectory("test").toFile();

0 commit comments

Comments
 (0)