question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue
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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
pschafflcommented, Mar 23, 2022
1reaction
marianobntz-silohubcommented, Feb 1, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle errors in ASP.NET Core web APIs - Microsoft Learn
This article describes how to handle errors and customize error handling with ASP.NET Core web APIs. Developer Exception Page.
Read more >
domaindrivendev/Swashbuckle.AspNetCore - GitHub
Swagger tooling for APIs built with ASP.NET Core. Generate beautiful API documentation, including a UI to explore and test operations, directly from your ......
Read more >
Get a template error when I try to do this? - Stack Overflow
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Read more >
Serene - Visual Studio Marketplace
Extension for Visual Studio - Serenity is an ASP.NET Core / TypeScript ... for custom types like dynamic, list etc. for code generation...
Read more >
ASP.NET Core Web API Best Practices - Code Maze
But wouldn't you agree that creating a project that works is not enough? Shouldn't that project be maintainable and readable as well? It...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found