Skip to content

Commit 26eb3f0

Browse files
committed
Fix dart-dio webhook imports generating Map-style strings (issue #22586)
Webhook operations were generating broken import statements with Map-style representation and HTML entity encoding: import '{import=model.Pet, classname=Pet}'; Instead of proper Dart package imports: import 'package:my-package/src/model/pet.dart'; This occurred because DartDioClientCodegen.postProcessOperationsWithModels() applied import processing to regular operations, but postProcessWebhooksWithModels() was missing the same logic. Fix: - Extracted shared import processing logic into processImports() method - Added postProcessWebhooksWithModels() override to apply same logic - Both operations and webhooks now use consistent import generation Test: - Added verifyWebhookImports() test using existing webhooks.yaml resource - Verifies generated code does not contain Map-style imports - Verifies generated code does not contain HTML entity encoding
1 parent 570915e commit 26eb3f0

2 files changed

Lines changed: 58 additions & 5 deletions

File tree

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.openapitools.codegen.model.ModelsMap;
3737
import org.openapitools.codegen.model.OperationMap;
3838
import org.openapitools.codegen.model.OperationsMap;
39+
import org.openapitools.codegen.model.WebhooksMap;
3940
import org.openapitools.codegen.templating.CommonTemplateContentLocator;
4041
import org.openapitools.codegen.templating.GeneratorTemplateContentLocator;
4142
import org.openapitools.codegen.templating.MustacheEngineAdapter;
@@ -649,8 +650,25 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
649650
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
650651
super.postProcessOperationsWithModels(objs, allModels);
651652
OperationMap operations = objs.getOperations();
652-
List<CodegenOperation> operationList = operations.getOperation();
653+
processImports(operations, operations.getOperation());
654+
return objs;
655+
}
656+
657+
@Override
658+
public WebhooksMap postProcessWebhooksWithModels(WebhooksMap objs, List<ModelMap> allModels) {
659+
super.postProcessWebhooksWithModels(objs, allModels);
660+
OperationMap operations = objs.getWebhooks();
661+
processImports(operations, operations.getOperation());
662+
return objs;
663+
}
653664

665+
/**
666+
* Processes imports for operations or webhooks, applying the same logic to both.
667+
*
668+
* @param operations the OperationMap containing the operations
669+
* @param operationList the list of CodegenOperation to process
670+
*/
671+
private void processImports(OperationMap operations, List<CodegenOperation> operationList) {
654672
Set<String> resultImports = new HashSet<>();
655673

656674
for (CodegenOperation op : operationList) {
@@ -687,7 +705,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
687705
if (SERIALIZATION_LIBRARY_JSON_SERIALIZABLE.equals(library)) {
688706
// built_value serialization uses Uint8List for all MultipartFile types
689707
// in json_serialization, MultipartFile is used as the file parameter type, but
690-
// MultipartFile isn't readable, instead we convert this to a Uin8List
708+
// MultipartFile isn't readable, instead we convert this to a Uint8List
691709
if (op.isResponseFile) {
692710
op.imports.add("Uint8List");
693711
op.returnType = "Uint8List";
@@ -729,9 +747,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
729747
}
730748
}
731749
// for some reason "import" structure is changed ..
732-
objs.put("imports", resultImports.stream().sorted().collect(Collectors.toList()));
733-
734-
return objs;
750+
operations.put("imports", resultImports.stream().sorted().collect(Collectors.toList()));
735751
}
736752

737753
private void addBuiltValueSerializerImport(String type) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/dart/dio/DartDioClientCodegenTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,41 @@ public void verifyDartDioGeneratorRuns() throws IOException {
115115
TestUtils.ensureContainsFile(files, output, "README.md");
116116
TestUtils.ensureContainsFile(files, output, "lib/src/api.dart");
117117
}
118+
119+
@Test
120+
public void verifyWebhookImports() throws IOException {
121+
File output = Files.createTempDirectory("test").toFile();
122+
output.deleteOnExit();
123+
124+
final CodegenConfigurator configurator = new CodegenConfigurator()
125+
.setGeneratorName("dart-dio")
126+
.setGitUserId("my-user")
127+
.setGitRepoId("my-repo")
128+
.setPackageName("my-package")
129+
.setInputSpec("src/test/resources/3_1/webhooks.yaml")
130+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
131+
132+
ClientOptInput opts = configurator.toClientOptInput();
133+
134+
Generator generator = new DefaultGenerator().opts(opts);
135+
List<File> files = generator.generate();
136+
files.forEach(File::deleteOnExit);
137+
138+
// Find webhook API file (default_api.dart for 'newPet' webhook)
139+
File apiDir = new File(output, "lib/src/api");
140+
Assert.assertTrue(apiDir.exists() && apiDir.isDirectory(), "API directory should exist");
141+
142+
File apiFile = new File(apiDir, "default_api.dart");
143+
Assert.assertTrue(apiFile.exists(), "default_api.dart should be generated for webhook");
144+
145+
String apiContent = Files.readString(apiFile.toPath(), StandardCharsets.UTF_8);
146+
147+
// Bug symptom #22586: Map-style import with HTML entity encoding
148+
// Before fix: import '{import&#x3D;model.Pet, classname&#x3D;Pet}';
149+
// After fix: import 'package:my_package/lib/src/gen/model/pet.dart';
150+
Assert.assertFalse(apiContent.contains("import '{"),
151+
"Webhook should not contain Map-style import (bug #22586 symptom)");
152+
Assert.assertFalse(apiContent.contains("&#x3D;"),
153+
"Webhook should not contain HTML entity encoding (bug #22586 symptom)");
154+
}
118155
}

0 commit comments

Comments
 (0)