-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
typescript: Fix Union Types Import Issue (#6789)
* First approach for discussion * typo * add addiotional method * polish a bit * remove call of super method * fix javadoc error * com.google.common.collect. * merge master regenerate samples * sort imports alphabetically * sort imports alphabetically with right key * typo * add type previous imports are still there. * add type previous imports are still there. * remove new test to see if they are the problem. * merge master add tests back in * align changes which should not lead to failing test but you never know. * remove formatting changes * dummy change * revert spaces * revert spaces * revert functional changes * comment out test * remove model * remove interface method * remove test class completely * put test class back - test body commented out * rename test methods * put back logic and tests * remove generated APIs * remark amakhrov * check in one generated file to test * adjust call super * add comment use set.
- Loading branch information
1 parent
71321bd
commit d3017ff
Showing
6 changed files
with
251 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...api-generator/src/test/java/org/openapitools/codegen/typescript/SharedTypeScriptTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.openapitools.codegen.typescript; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.openapitools.codegen.DefaultGenerator; | ||
import org.openapitools.codegen.Generator; | ||
import org.openapitools.codegen.config.CodegenConfigurator; | ||
import org.openapitools.codegen.languages.TypeScriptAxiosClientCodegen; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class SharedTypeScriptTest { | ||
@Test | ||
public void typesInImportsAreSplittedTest() throws IOException { | ||
CodegenConfigurator config = | ||
new CodegenConfigurator() | ||
.setInputSpec("src/test/resources/split-import.json") | ||
.setModelPackage("model") | ||
.setApiPackage("api") | ||
.setOutputDir("src/test/resources/typesInImportsAreSplittedTest") | ||
.addAdditionalProperty( | ||
TypeScriptAxiosClientCodegen.SEPARATE_MODELS_AND_API, true); | ||
|
||
config.setGeneratorName("typescript-axios"); | ||
checkAPIFile(getGenerator(config).generate(), "default-api.ts"); | ||
|
||
config.setGeneratorName("typescript-node"); | ||
checkAPIFile(getGenerator(config).generate(), "defaultApi.ts"); | ||
|
||
config.setGeneratorName("typescript-angular"); | ||
checkAPIFile(getGenerator(config).generate(), "default.service.ts"); | ||
|
||
FileUtils.deleteDirectory(new File("src/test/resources/typesInImportsAreSplittedTest")); | ||
} | ||
|
||
private Generator getGenerator(CodegenConfigurator config) { | ||
return new DefaultGenerator().opts(config.toClientOptInput()); | ||
} | ||
|
||
private void checkAPIFile(List<File> files, String apiFileName) throws IOException { | ||
File apiFile = files.stream().filter(file->file.getName().contains(apiFileName)).findFirst().get(); | ||
String apiFileContent = FileUtils.readFileToString(apiFile); | ||
Assert.assertTrue(!apiFileContent.contains("import { OrganizationWrapper | PersonWrapper }")); | ||
Assert.assertEquals(StringUtils.countMatches(apiFileContent,"import { PersonWrapper }"),1); | ||
Assert.assertEquals(StringUtils.countMatches(apiFileContent,"import { OrganizationWrapper }"),1); | ||
} | ||
|
||
@Test | ||
public void oldImportsStillPresentTest() throws IOException { | ||
CodegenConfigurator config = | ||
new CodegenConfigurator() | ||
.setInputSpec("petstore.json") | ||
.setModelPackage("model") | ||
.setApiPackage("api") | ||
.setOutputDir("src/test/resources/oldImportsStillPresentTest/") | ||
.addAdditionalProperty( | ||
TypeScriptAxiosClientCodegen.SEPARATE_MODELS_AND_API, true); | ||
|
||
config.setGeneratorName("typescript-angular"); | ||
final List<File> files = getGenerator(config).generate(); | ||
File pets = files.stream().filter(file->file.getName().contains("pet.ts")).findFirst().get(); | ||
String apiFileContent = FileUtils.readFileToString(pets); | ||
Assert.assertTrue(apiFileContent.contains("import { Category }")); | ||
Assert.assertTrue(apiFileContent.contains("import { Tag }")); | ||
|
||
FileUtils.deleteDirectory(new File("src/test/resources/oldImportsStillPresentTest/")); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
modules/openapi-generator/src/test/resources/split-import.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"openapi": "3.0.2", | ||
"info": { | ||
"title": "SAP Graph - Customers API", | ||
"version": "0.1.0" | ||
}, | ||
"paths": { | ||
"/Customer": { | ||
"get": { | ||
"operationId": "getCustomer", | ||
"responses": { | ||
"200": { | ||
"$ref": "#/components/responses/CustomerResponse" | ||
} | ||
} | ||
} | ||
}, | ||
"/Person": { | ||
"get": { | ||
"operationId": "getPerson", | ||
"responses": { | ||
"200": { | ||
"$ref": "#/components/responses/PersonResponse" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"OrganizationWrapper": { | ||
"type": "object", | ||
"properties": { | ||
"id": {"type": "string"} | ||
} | ||
}, | ||
"PersonWrapper": { | ||
"type": "object", | ||
"properties": { | ||
"id": {"type": "string"} | ||
} | ||
} | ||
}, | ||
"responses": { | ||
"CustomerResponse": { | ||
"description": "Get Customer", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"oneOf": [ | ||
{ | ||
"$ref": "#/components/schemas/OrganizationWrapper" | ||
}, | ||
{ | ||
"$ref": "#/components/schemas/PersonWrapper" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
"PersonResponse": { | ||
"description": "Get Person", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/PersonWrapper" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |