Skip to content

[BUG][aspnetcore] Generating with custom templates of type APITests fails #8598

Open
@adrian-ilie

Description

@adrian-ilie
Description

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));
}

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions