Skip to content

[BUG][Python] Adding custom model template with Python generator throws exception #8420

@phemmer

Description

@phemmer

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When adding a custom model template with the Python generator, an exception is thrown.

openapi-generator version

5.0.0

OpenAPI declaration file content or url
# api.yaml
openapi: 3.0.3
info:
  title: API
  description: API
  version: 1.0.0
servers:
  - url: 'https://localhost:80'
paths: {}
components:
  schemas:
    Test:
      type: object
Generation Details

Following the instructions from https://github.com/OpenAPITools/openapi-generator/blob/master/docs/customization.md:

# config.yaml
templateDir: templates
files:
  model-stub.mustache:
    templateType: Model
    destinationFilename: .pyi
$ java -jar openapi-generator-cli-5.0.0.jar generate -g python -c config.yaml -i api.yaml -o gen/
[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] WARN  o.o.c.ignore.CodegenIgnoreProcessor - Output directory does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated.
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: python (client)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'python' is considered experimental.
[main] INFO  o.o.c.l.PythonLegacyClientCodegen - Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE="/usr/local/bin/yapf -i"' (Linux/Mac)
[main] INFO  o.o.c.l.PythonLegacyClientCodegen - NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] INFO  o.o.c.languages.PythonClientCodegen - generateAliasAsModel is hard coded to true in this generator. Alias models will only be generated if they contain validations or enums
Exception in thread "main" java.lang.IllegalStateException: Duplicate key SupportingFile[templateFile='__init__model.mustache', folder='openapi_client/models', destinationFilename='__init__.py', canOverwrite=true]
	at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
	at java.util.HashMap.merge(HashMap.java:1254)
	at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
	at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
	at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566)
	at org.openapitools.codegen.DefaultGenerator.processUserDefinedTemplates(DefaultGenerator.java:941)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:869)
	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)
Steps to reproduce
  1. Create the above api.yaml.
  2. Create the above config.yaml.
  3. Create an empty templates directory.
  4. Run the above generate command.
Expected result

Should create a gen/openapi_client/model/test.pyi file according to templates/model-stub.mustache.

Related issues/PRs
Suggest a fix

It would appear that the use of templateType: model is causing the generator to add __init__model.mustache to the file list multiple times. There does not appear to be a way to prevent this.
As I am not redefining or providing a template for __init__model.mustache, it should not be trying to add it again.

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