OpenAPITools / OpenAPITools/openapi-generator

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

Open
#8,598 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: C-Sharp
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java, especially processTemplateToFile and the APITests generation path. Reproduce the issue with the provided config and Windows paths; done means the custom APITests template generates the expected files without the output-directory exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.