Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,20 @@ void generateModels(List<File> files, List<ModelMap> allModels, List<String> unu
allModels.add(modelTemplate);
}

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @OpenAPITools/generator-core-team

// to generate model files
generateModel(files, models, modelName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,18 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
CodegenModel cm = mo.getModel();
cm.imports = rewriteImports(cm.imports, true);
cm.vendorExtensions.put("x-has-vars", !cm.vars.isEmpty());

// Check if this model's classname has an import mapping.
// If so, mark it so that supporting file templates (serializers, barrel)
// can use the mapped import path instead of the default model/ path.
if (importMapping().containsKey(cm.classname)) {
cm.vendorExtensions.put("x-is-import-mapped", true);
cm.vendorExtensions.put("x-import-path", importMapping().get(cm.classname));
} else {
cm.vendorExtensions.put("x-is-import-mapped", false);
cm.vendorExtensions.put("x-import-path",
"package:" + pubName + "/" + sourceFolder + "/" + modelPackage() + "/" + cm.classFilename + ".dart");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export 'package:{{pubName}}/{{sourceFolder}}/auth/oauth.dart';

{{#apiInfo}}{{#apis}}export 'package:{{pubName}}/{{sourceFolder}}/{{apiPackage}}/{{classFilename}}.dart';
{{/apis}}{{/apiInfo}}
{{#models}}{{#model}}export 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/{{classFilename}}.dart';
{{#models}}{{#model}}export '{{{vendorExtensions.x-import-path}}}';
{{/model}}{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:{{pubName}}/{{sourceFolder}}/date_serializer.dart';
import 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/date.dart';{{/useDateLibCore}}
{{#useDateLibTimeMachine}}import 'package:time_machine/time_machine.dart';
import 'package:{{pubName}}/{{sourceFolder}}/offset_date_serializer.dart';{{/useDateLibTimeMachine}}
{{#models}}{{#model}}import 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/{{classFilename}}.dart';
{{#models}}{{#model}}import '{{{vendorExtensions.x-import-path}}}';
{{/model}}{{/models}}{{#builtValueSerializerImports}}import '{{{.}}}';
{{/builtValueSerializerImports}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#models}}
{{#model}}
{{^isEnum}}
import 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/{{classFilename}}.dart';
import '{{{vendorExtensions.x-import-path}}}';
{{/isEnum}}
{{/model}}
{{/models}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -93,6 +94,54 @@ public void testKeywords() {
}
}

@Test
public void testImportMappingsInSerializersAndBarrelFile() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final String pubName = "my_api";
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("dart-dio")
.setGitUserId("my-user")
.setGitRepoId("my-repo")
.setInputSpec("src/test/resources/3_0/dart-dio/import_mapping.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

configurator.addAdditionalProperty("pubName", pubName);
configurator.addTypeMapping("Address", "CustomAddress");
configurator.addImportMapping("CustomAddress", "package:my_api/src/custom_models/custom_address.dart");

ClientOptInput opts = configurator.toClientOptInput();

Generator generator = new DefaultGenerator().opts(opts);
List<File> files = generator.generate();
files.forEach(File::deleteOnExit);

// The model file for the mapped type should NOT be generated
TestUtils.assertFileNotExists(output.toPath().resolve("lib/src/model/custom_address.dart"));

// order_out.dart should use the custom import path (this already works per the issue report)
Path orderOutPath = output.toPath().resolve("lib/src/model/order_out.dart");
TestUtils.assertFileContains(orderOutPath,
"package:my_api/src/custom_models/custom_address.dart");
TestUtils.assertFileNotContains(orderOutPath,
"package:my_api/src/model/custom_address.dart");

// serializers.dart should use the custom import path, not the hardcoded model/ path
Path serializersPath = output.toPath().resolve("lib/src/serializers.dart");
TestUtils.assertFileContains(serializersPath,
"package:my_api/src/custom_models/custom_address.dart");
TestUtils.assertFileNotContains(serializersPath,
"package:my_api/src/model/custom_address.dart");

// The barrel file should use the custom export path, not the hardcoded model/ path
Path barrelPath = output.toPath().resolve("lib/my_api.dart");
TestUtils.assertFileContains(barrelPath,
"package:my_api/src/custom_models/custom_address.dart");
TestUtils.assertFileNotContains(barrelPath,
"package:my_api/src/model/custom_address.dart");
}

@Test
public void verifyDartDioGeneratorRuns() throws IOException {
File output = Files.createTempDirectory("test").toFile();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: "3.1.0"
info:
title: Example
version: "1.0.0"
paths:
/orders:
get:
operationId: getOrders
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/OrderOut"
components:
schemas:
Address:
type: object
required: [street, city]
properties:
street:
type: string
city:
type: string
OrderOut:
type: object
required: [id, shippingAddress]
properties:
id:
type: integer
shippingAddress:
$ref: "#/components/schemas/Address"
Loading