Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #8598
Closed
Open
Issue created Feb 02, 2021 by Administrator@rootContributor

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

Created by: 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

Assignee
Assign to
Time tracking