Skip to content

Commit 388f031

Browse files
authored
Fix 23558 (#23571)
1 parent 46714d4 commit 388f031

7 files changed

Lines changed: 111 additions & 3 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,20 @@ void generateModels(List<File> files, List<ModelMap> allModels, List<String> unu
570570
allModels.add(modelTemplate);
571571
}
572572

573+
// Don't generate model files for types that were explicitly type-mapped
574+
// AND whose mapped name has a corresponding import mapping.
575+
// This indicates the user wants to replace the schema type with an
576+
// external type (e.g. --type-mappings Address=CustomAddress
577+
// --import-mappings CustomAddress=package:custom/address.dart).
578+
// The model metadata is still kept in allModels for use by supporting file templates.
579+
if (config.typeMapping().containsKey(modelName)) {
580+
String mappedTypeName = config.typeMapping().get(modelName);
581+
if (config.importMapping().containsKey(mappedTypeName)) {
582+
LOGGER.info("Model {} (type-mapped to {}) not generated due to import mapping", modelName, mappedTypeName);
583+
continue;
584+
}
585+
}
586+
573587
// to generate model files
574588
generateModel(files, models, modelName);
575589

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,18 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
617617
CodegenModel cm = mo.getModel();
618618
cm.imports = rewriteImports(cm.imports, true);
619619
cm.vendorExtensions.put("x-has-vars", !cm.vars.isEmpty());
620+
621+
// Check if this model's classname has an import mapping.
622+
// If so, mark it so that supporting file templates (serializers, barrel)
623+
// can use the mapped import path instead of the default model/ path.
624+
if (importMapping().containsKey(cm.classname)) {
625+
cm.vendorExtensions.put("x-is-import-mapped", true);
626+
cm.vendorExtensions.put("x-import-path", importMapping().get(cm.classname));
627+
} else {
628+
cm.vendorExtensions.put("x-is-import-mapped", false);
629+
cm.vendorExtensions.put("x-import-path",
630+
"package:" + pubName + "/" + sourceFolder + "/" + modelPackage() + "/" + cm.classFilename + ".dart");
631+
}
620632
}
621633
}
622634

modules/openapi-generator/src/main/resources/dart/libraries/dio/lib.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ export 'package:{{pubName}}/{{sourceFolder}}/auth/oauth.dart';
99

1010
{{#apiInfo}}{{#apis}}export 'package:{{pubName}}/{{sourceFolder}}/{{apiPackage}}/{{classFilename}}.dart';
1111
{{/apis}}{{/apiInfo}}
12-
{{#models}}{{#model}}export 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/{{classFilename}}.dart';
12+
{{#models}}{{#model}}export '{{{vendorExtensions.x-import-path}}}';
1313
{{/model}}{{/models}}

modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/serializers.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:{{pubName}}/{{sourceFolder}}/date_serializer.dart';
1212
import 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/date.dart';{{/useDateLibCore}}
1313
{{#useDateLibTimeMachine}}import 'package:time_machine/time_machine.dart';
1414
import 'package:{{pubName}}/{{sourceFolder}}/offset_date_serializer.dart';{{/useDateLibTimeMachine}}
15-
{{#models}}{{#model}}import 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/{{classFilename}}.dart';
15+
{{#models}}{{#model}}import '{{{vendorExtensions.x-import-path}}}';
1616
{{/model}}{{/models}}{{#builtValueSerializerImports}}import '{{{.}}}';
1717
{{/builtValueSerializerImports}}
1818

modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/deserialize.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{#models}}
22
{{#model}}
33
{{^isEnum}}
4-
import 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/{{classFilename}}.dart';
4+
import '{{{vendorExtensions.x-import-path}}}';
55
{{/isEnum}}
66
{{/model}}
77
{{/models}}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.*;
2626
import java.nio.charset.StandardCharsets;
2727
import java.nio.file.Files;
28+
import java.nio.file.Path;
2829
import java.util.ArrayList;
2930
import java.util.List;
3031
import java.util.Locale;
@@ -93,6 +94,54 @@ public void testKeywords() {
9394
}
9495
}
9596

97+
@Test
98+
public void testImportMappingsInSerializersAndBarrelFile() throws IOException {
99+
File output = Files.createTempDirectory("test").toFile();
100+
output.deleteOnExit();
101+
102+
final String pubName = "my_api";
103+
final CodegenConfigurator configurator = new CodegenConfigurator()
104+
.setGeneratorName("dart-dio")
105+
.setGitUserId("my-user")
106+
.setGitRepoId("my-repo")
107+
.setInputSpec("src/test/resources/3_0/dart-dio/import_mapping.yaml")
108+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
109+
110+
configurator.addAdditionalProperty("pubName", pubName);
111+
configurator.addTypeMapping("Address", "CustomAddress");
112+
configurator.addImportMapping("CustomAddress", "package:my_api/src/custom_models/custom_address.dart");
113+
114+
ClientOptInput opts = configurator.toClientOptInput();
115+
116+
Generator generator = new DefaultGenerator().opts(opts);
117+
List<File> files = generator.generate();
118+
files.forEach(File::deleteOnExit);
119+
120+
// The model file for the mapped type should NOT be generated
121+
TestUtils.assertFileNotExists(output.toPath().resolve("lib/src/model/custom_address.dart"));
122+
123+
// order_out.dart should use the custom import path (this already works per the issue report)
124+
Path orderOutPath = output.toPath().resolve("lib/src/model/order_out.dart");
125+
TestUtils.assertFileContains(orderOutPath,
126+
"package:my_api/src/custom_models/custom_address.dart");
127+
TestUtils.assertFileNotContains(orderOutPath,
128+
"package:my_api/src/model/custom_address.dart");
129+
130+
// serializers.dart should use the custom import path, not the hardcoded model/ path
131+
Path serializersPath = output.toPath().resolve("lib/src/serializers.dart");
132+
TestUtils.assertFileContains(serializersPath,
133+
"package:my_api/src/custom_models/custom_address.dart");
134+
TestUtils.assertFileNotContains(serializersPath,
135+
"package:my_api/src/model/custom_address.dart");
136+
137+
// The barrel file should use the custom export path, not the hardcoded model/ path
138+
Path barrelPath = output.toPath().resolve("lib/my_api.dart");
139+
TestUtils.assertFileContains(barrelPath,
140+
"package:my_api/src/custom_models/custom_address.dart");
141+
TestUtils.assertFileNotContains(barrelPath,
142+
"package:my_api/src/model/custom_address.dart");
143+
}
144+
96145
@Test
97146
public void verifyDartDioGeneratorRuns() throws IOException {
98147
File output = Files.createTempDirectory("test").toFile();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: "3.1.0"
2+
info:
3+
title: Example
4+
version: "1.0.0"
5+
paths:
6+
/orders:
7+
get:
8+
operationId: getOrders
9+
responses:
10+
"200":
11+
description: OK
12+
content:
13+
application/json:
14+
schema:
15+
$ref: "#/components/schemas/OrderOut"
16+
components:
17+
schemas:
18+
Address:
19+
type: object
20+
required: [street, city]
21+
properties:
22+
street:
23+
type: string
24+
city:
25+
type: string
26+
OrderOut:
27+
type: object
28+
required: [id, shippingAddress]
29+
properties:
30+
id:
31+
type: integer
32+
shippingAddress:
33+
$ref: "#/components/schemas/Address"

0 commit comments

Comments
 (0)