Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[html2] Support alias types #18579

Merged
merged 4 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ private void generateModel(List<File> files, Map<String, Object> models, String
}
}

void generateModels(List<File> files, List<ModelMap> allModels, List<String> unusedModels) {
void generateModels(List<File> files, List<ModelMap> allModels, List<String> unusedModels, List<ModelMap> aliasModels) {
if (!generateModels) {
// TODO: Process these anyway and add to dryRun info
LOGGER.info("Skipping generation of models.");
Expand Down Expand Up @@ -567,6 +567,8 @@ void generateModels(List<File> files, List<ModelMap> allModels, List<String> unu
CodegenModel m = modelTemplate.getModel();
if (m.isAlias) {
// alias to number, string, enum, etc, which should not be generated as model
// but aliases are still used to dereference models in some languages (such as in html2).
aliasModels.add(modelTemplate); // Store aliases in the separate list.
continue; // Don't create user-defined classes for aliases
}
}
Expand Down Expand Up @@ -1074,11 +1076,11 @@ private void generateSupportingFiles(List<File> files, Map<String, Object> bundl
generateVersionMetadata(files);
}

Map<String, Object> buildSupportFileBundle(List<OperationsMap> allOperations, List<ModelMap> allModels) {
return this.buildSupportFileBundle(allOperations, allModels, null);
Map<String, Object> buildSupportFileBundle(List<OperationsMap> allOperations, List<ModelMap> allModels, List<ModelMap> aliasModels) {
return this.buildSupportFileBundle(allOperations, allModels, aliasModels, null);
}

Map<String, Object> buildSupportFileBundle(List<OperationsMap> allOperations, List<ModelMap> allModels, List<WebhooksMap> allWebhooks) {
Map<String, Object> buildSupportFileBundle(List<OperationsMap> allOperations, List<ModelMap> allModels, List<ModelMap> aliasModels, List<WebhooksMap> allWebhooks) {

Map<String, Object> bundle = new HashMap<>(config.additionalProperties());
bundle.put("apiPackage", config.apiPackage());
Expand All @@ -1100,6 +1102,7 @@ Map<String, Object> buildSupportFileBundle(List<OperationsMap> allOperations, Li
bundle.put("apiInfo", apis);
bundle.put("webhooks", allWebhooks);
bundle.put("models", allModels);
bundle.put("aliasModels", aliasModels);
bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar));
bundle.put("modelPackage", config.modelPackage());
bundle.put("library", config.getLibrary());
Expand Down Expand Up @@ -1225,15 +1228,16 @@ public List<File> generate() {
// models
List<String> filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
List<ModelMap> allModels = new ArrayList<>();
generateModels(files, allModels, filteredSchemas);
List<ModelMap> aliasModels = new ArrayList<>();
generateModels(files, allModels, filteredSchemas, aliasModels);
// apis
List<OperationsMap> allOperations = new ArrayList<>();
generateApis(files, allOperations, allModels);
// webhooks
List<WebhooksMap> allWebhooks = new ArrayList<>();
generateWebhooks(files, allWebhooks, allModels);
// supporting files
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels, allWebhooks);
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels, aliasModels, allWebhooks);
Copy link
Member

Choose a reason for hiding this comment

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

fyi @OpenAPITools/generator-core-team

generateSupportingFiles(files, bundle);

if (dryRun) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@
{{/model}}
{{/models}}

{{#aliasModels}}
{{#model}}
defs["{{name}}"] = {{{modelJson}}};
{{/model}}
{{/aliasModels}}

var errs = {};
{{#swagger.vendorExtensions.x-shared-errors}}
{
Expand Down Expand Up @@ -459,11 +465,13 @@
}
if (schema.$ref != null) {
schema = defsParser.$refs.get(schema.$ref);
Object.keys(schema.properties).forEach( (item) => {
if (schema.properties[item].$ref != null) {
schema.properties[item] = defsParser.$refs.get(schema.properties[item].$ref);
}
});
if (schema.properties != null) {
Object.keys(schema.properties).forEach( (item) => {
if (schema.properties[item].$ref != null) {
schema.properties[item] = defsParser.$refs.get(schema.properties[item].$ref);
}
});
}
} else if (schema.items != null && schema.items.$ref != null) {
schema.items = defsParser.$refs.get(schema.items.$ref);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,8 @@ public void verifyXDiscriminatorValue() {
List<File> files = new ArrayList<>();
List<String> filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
List<ModelMap> allModels = new ArrayList<>();
generator.generateModels(files, allModels, filteredSchemas);
List<ModelMap> aliasModels = new ArrayList<>();
generator.generateModels(files, allModels, filteredSchemas, aliasModels);

// check that the model's children contain the x-discriminator-values
modelName = "BaseObj";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,11 +660,12 @@ public void testHandlesTrailingSlashInServers() {
List<File> files = new ArrayList<>();
List<String> filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
List<ModelMap> allModels = new ArrayList<>();
generator.generateModels(files, allModels, filteredSchemas);
List<ModelMap> aliasModels = new ArrayList<>();
generator.generateModels(files, allModels, filteredSchemas, aliasModels);
List<OperationsMap> allOperations = new ArrayList<>();
generator.generateApis(files, allOperations, allModels);

Map<String, Object> bundle = generator.buildSupportFileBundle(allOperations, allModels);
Map<String, Object> bundle = generator.buildSupportFileBundle(allOperations, allModels, aliasModels);
LinkedList<CodegenServer> servers = (LinkedList<CodegenServer>) bundle.get("servers");
Assert.assertEquals(servers.get(0).url, "");
Assert.assertEquals(servers.get(1).url, "http://trailingshlash.io:80/v1");
Expand All @@ -686,11 +687,12 @@ public void testHandlesRelativeUrlsInServers() {
List<File> files = new ArrayList<>();
List<String> filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
List<ModelMap> allModels = new ArrayList<>();
generator.generateModels(files, allModels, filteredSchemas);
List<ModelMap> aliasModels = new ArrayList<>();
generator.generateModels(files, allModels, filteredSchemas, aliasModels);
List<OperationsMap> allOperations = new ArrayList<>();
generator.generateApis(files, allOperations, allModels);

Map<String, Object> bundle = generator.buildSupportFileBundle(allOperations, allModels);
Map<String, Object> bundle = generator.buildSupportFileBundle(allOperations, allModels, aliasModels);
LinkedList<CodegenServer> servers = (LinkedList<CodegenServer>) bundle.get("servers");
Assert.assertEquals(servers.get(0).url, "/relative/url");
}
Expand Down Expand Up @@ -768,8 +770,9 @@ public void testRecursionBug4650() {
List<File> files = new ArrayList<>();
List<String> filteredSchemas = ModelUtils.getSchemasUsedOnlyInFormParam(openAPI);
List<ModelMap> allModels = new ArrayList<>();
List<ModelMap> aliasModels = new ArrayList<>();
// The bug causes a StackOverflowError when calling generateModels
generator.generateModels(files, allModels, filteredSchemas);
generator.generateModels(files, allModels, filteredSchemas, aliasModels);
// all fine, we have passed
}
}