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 @@ -407,13 +407,16 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}
if (!cm.oneOf.isEmpty()) {
// For oneOfs only import $refs within the oneOf
TreeSet<String> oneOfRefs = new TreeSet<>();
for (String im : cm.imports) {
if (cm.oneOf.contains(im)) {
oneOfRefs.add(im);
}
TreeSet<String> oneOfRefs = new TreeSet<>(cm.oneOf);
List<CodegenProperty> oneOfProperties = Optional.ofNullable(cm.getComposedSchemas())
.map(CodegenComposedSchemas::getOneOf)
.orElse(Collections.emptyList());
for (CodegenProperty oneOfProperty : oneOfProperties) {
// resolve the types referenced by each branch so that inner types of
// container types (e.g. Foo in Array<Foo>) keep their model import
oneOfRefs.addAll(oneOfProperty.getImports(false, true, generatorMetadata.getFeatureSet()));
}
cm.imports = oneOfRefs;
cm.imports = new TreeSet<>(Sets.intersection(cm.imports, oneOfRefs));
}
}
for (ModelMap mo : models) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import org.openapitools.codegen.utils.ModelUtils;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeSet;

/**
Expand Down Expand Up @@ -148,13 +150,14 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}
if (!cm.oneOf.isEmpty()) {
// For oneOfs only import $refs within the oneOf
TreeSet<String> oneOfRefs = new TreeSet<>();
for (String im : cm.imports) {
if (cm.oneOf.contains(im)) {
oneOfRefs.add(im);
}
TreeSet<String> oneOfRefs = new TreeSet<>(cm.oneOf);
List<CodegenProperty> oneOfProperties = Optional.ofNullable(cm.getComposedSchemas())
.map(CodegenComposedSchemas::getOneOf)
.orElse(Collections.emptyList());
for (CodegenProperty oneOfProperty : oneOfProperties) {
oneOfRefs.addAll(oneOfProperty.getImports(false, true, generatorMetadata.getFeatureSet()));
}
cm.imports = oneOfRefs;
cm.imports.retainAll(oneOfRefs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,34 @@ public void testDeprecatedAttribute() throws Exception {
assertEquals(1, TestUtils.countOccurrences(content, "@deprecated"));
}

@Test
public void testCircularOneOfGeneratesImports() throws Exception {
final File output = Files.createTempDirectory("typescriptclient_").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("typescript")
.setInputSpec("src/test/resources/bugs/issue_23286.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
final DefaultGenerator generator = new DefaultGenerator();
final List<File> files = generator.opts(clientOptInput).generate();
files.forEach(File::deleteOnExit);

// model with a circular oneOf branch (Array<Repro>) must import Repro
TestUtils.assertFileContains(
Paths.get(output + "/models/ReproContent.ts"),
"import { Repro } from '../models/Repro';",
"export type ReproContent = Array<Repro> | string;"
);

configurator.setGeneratorName("typescript-redux-query");
generator.opts(configurator.toClientOptInput()).generate().forEach(File::deleteOnExit);
TestUtils.assertFileContains(Paths.get(output + "/src/models/ReproContent.ts"),
"Repro,", "export type ReproContent = Array<Repro> | string;");
}

@Test(description = "Verify useErasableSyntax config parameter generates erasable code")
public void testUseErasableSyntaxConfig() throws IOException {
boolean[] options = {true, false};
Expand Down
16 changes: 16 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_23286.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
openapi: 3.0.3
info:
title: Repro
version: 1.0.0
paths: {}
components:
schemas:
Repro:
type: object
properties:
content:
oneOf:
- type: string
- type: array
items:
$ref: '#/components/schemas/Repro'
Loading