[BUG][aspnetcore] Generating with custom templates of type APITests fails
See original GitHub issueDescription
The aspnetcore generator does not support generating api tests out of the box from what I can see so I am trying to generate my own based on custom templates, following the instructions detailed here.
When running the generator I get back this exception and no test files are generated:
Exception in thread "main" java.lang.RuntimeException: Could not generate api file for 'Address' at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:673) at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:876) at org.openapitools.codegen.cmd.Generate.execute(Generate.java:432) at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32) at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66) Caused by: java.lang.RuntimeException: Target files must be generated within the output directory; absoluteTarget=C:\Projects\OpenApiGenerator\outdir.Test\AddressApiTests.cs outDir=C:\Projects\OpenApiGenerator\outdir at org.openapitools.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:1010) at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:650)
openapi-generator version
5.0.0 or 5.0.0-beta3
Generation Details
config.yaml contents:
templateDir: C:\Projects\OpenApiGenerator\CustomTemplates additionalProperties: dateLibrary: java8 files: apiTest.mustache: templateType: APITests destinationFilename: Test.cs
Command used to generate:
java -jar openapi-generator-cli.jar generate -g aspnetcore -i openapi.yaml -o outdir -c config.yaml --global-property apis,apiTests=true
Steps to reproduce
Create a directory to place your custom mustache template file apiTest.mustache in there, reference the file and the mustache as in a config.yaml as above. Run the command from above. I would expect to see a test file for each operation defined in the openapi specification but instead no file is created and an exception is thrown.
Suggest a fix
There is a condition in the DefaultGenerator for the absoluteTarget to start with outputDir. When using APITests template type it will try to write the file in outdir.Test folder. Comparing two Path objects is not the same as comparing strings, so maybe this condition could be adjusted? absoluteTarget=C:\Projects\OpenApiGenerator\outdir.Test\AddressApiTests.cs outDir =C:\Projects\OpenApiGenerator\outdir
if (!absoluteTarget.startsWith(outDir)) { throw new RuntimeException(String.format(Locale.ROOT, “Target files must be generated within the output directory; absoluteTarget=%s outDir=%s”, absoluteTarget, outDir)); }
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5
Top GitHub Comments
generateControllerAsAbstract=true seems to fixed by https://github.com/OpenAPITools/openapi-generator/commit/47f07158a37a83680a4d04c7d385b8e309f4879c
This problem is still happening in generator version 5.4.0 and cli 2.4.25… I am using the java-micronaut-server generator and got the same error…
Exception in thread "main" java.lang.RuntimeException: Could not generate api file for 'Tenant' at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:665) at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:891) at org.openapitools.codegen.cmd.Generate.execute(Generate.java:441) at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32) at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66) Caused by: java.lang.RuntimeException: Target files must be generated within the output directory; absoluteTarget=/Users/mariano/work/silohub/product-apis/build/server/bff-retailer/v0/src/main/java/ag/silohub/server/tenant/controller/TenantController.java outDir=/Users/mariano/work/silohub/product-apis/build/server/bff-retailer/v0/src/test/java/ag/silohub/server/tenant at org.openapitools.codegen.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:1032) at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:642) ... 4 more