Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core][feature] User custom added templates #7366

Merged
merged 6 commits into from
Sep 12, 2020

Conversation

jimschubert
Copy link
Member

Users would previously be forced to extend their target generator and go
through more complicated customization steps just to add new files to
generated output.

This adds support for user-defined templates.

Users can now:

  • add supporting files that aren't referenced in the target generator
  • include additional templates for api/model and their docs and
    templates
  • modify file extensions/suffixes for api/model (docs+tests)

Notes on the new feature:

  • This is currently a "merge" operation. Existing definitions will be
    overwritten, but other template definitions will be untouched. This
    allows users to add files by default.
  • To modify generated suffixes/extensions for others (not supporting
    files), may require inspection of source to exactly match the map key
    used in the target generator. e.g. "model_doc.mustache", "Doc.md"
    would output DogDoc.md. If the target generator includes
    "model_doc.mustache" and user passes "model-doc.mustache", this will
    result in duplicate docs being generated. We don't currently have a
    mechanism in place to inspect these.

See customization.md changes in this PR for full details. A quick example:

generatorName: kotlin
outputDir: samples/client/petstore/kotlin-user-customized-templates
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
templateDir: .bak/kotlin-templates
library: jvm-okhttp3
additionalProperties:
  artifactId: kotlin-petstore-client
  serializableModel: "true"
  dateLibrary: java8
files:
  AUTHORS.md: {}
  api_interfaces.mustache:
    templateType: API
    destinationFilename: Interface.kt
  api.mustache:
    templateType: API
    destinationFilename: Impl.kt

Modifying bin/configs/kotlin.yaml with the above, we can define the following files:

  • .bak/kotlin-templates/AUTHORS.md (not included in the Kotlin generator)
  • .bak/kotlin-templates/libraries/jvm-okhttp/api_interfaces.mustache (not included in the Kotlin generator)

The above will generate an AUTHORS.md, a user customized interface output for each API type generated, and will change the file suffix for the built-in api.mustache from .kt to Impl.kt.

Full output tree of the above config would look like this:

├── AUTHORS.md
├── README.md
├── build.gradle
├── docs
│   ├── ApiResponse.md
│   ├── Category.md
│   ├── Order.md
│   ├── Pet.md
│   ├── PetApi.md
│   ├── StoreApi.md
│   ├── Tag.md
│   ├── User.md
│   └── UserApi.md
├── settings.gradle
└── src
    └── main
        └── kotlin
            └── org
                └── openapitools
                    └── client
                        ├── apis
                        │   ├── PetApiImpl.kt
                        │   ├── PetApiInterface.kt
                        │   ├── StoreApiImpl.kt
                        │   ├── StoreApiInterface.kt
                        │   ├── UserApiImpl.kt
                        │   └── UserApiInterface.kt
                        ├── infrastructure
                        │   ├── ApiAbstractions.kt
                        │   ├── ApiClient.kt
                        │   ├── ApiInfrastructureResponse.kt
                        │   ├── ApplicationDelegates.kt
                        │   ├── ByteArrayAdapter.kt
                        │   ├── Errors.kt
                        │   ├── LocalDateAdapter.kt
                        │   ├── LocalDateTimeAdapter.kt
                        │   ├── OffsetDateTimeAdapter.kt
                        │   ├── RequestConfig.kt
                        │   ├── RequestMethod.kt
                        │   ├── ResponseExtensions.kt
                        │   ├── Serializer.kt
                        │   └── UUIDAdapter.kt
                        └── models
                            ├── ApiResponse.kt
                            ├── Category.kt
                            ├── Order.kt
                            ├── Pet.kt
                            ├── Tag.kt
                            └── User.kt

10 directories, 39 files

Note the AUTHORS.md and types/files under package org.openapitools.client.apis.

cc @OpenAPITools/generator-core-team

Potential breaking change:
This encapsulates properties of SupportingFiles. For users consuming programmatically via Java, this will require updating direct field accessors to getter/setters; Kotlin users consuming programmatically will be unaffected.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

This allows encapsulation of SupportingFile properties rather than
direct field access. TemplateDefinition will be used as the base to
allow for different types of supporting file extensions.
Users would previously be forced to extend their target generator and go
through more complicated customization steps just to add new files to
generated output.

This adds support for user-defined templates.

Users can now:

* add supporting files that aren't referenced in the target generator
* include additional templates for api/model and their docs and
  templates
* modify file extensions/suffixes for api/model (docs+tests)

Notes on the new feature:

* This is currently a "merge" operation. Existing definitions will be
  overwritten, but other template definitions will be untouched. This
  allows users to add files by default.
* To modify generated suffixes/extensions for others (not supporting
  files), may require inspection of source to exactly match the map key
  used in the target generator. e.g. "model_doc.mustache", "Doc.md"
  would output DogDoc.md. If the target generator includes
  "model_doc.mustache" and user passes "model-doc.mustache", this will
  result in duplicate docs being generated. We don't currently have a
  mechanism in place to inspect these.
@auto-labeler
Copy link

auto-labeler bot commented Sep 7, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

This removes TemplateFile and roles the type enum property into
TemplateDefinition.
@wing328
Copy link
Member

wing328 commented Sep 8, 2020

FYI. DroneCI reports the following errors:


+ /bin/bash bin/utils/test-fake-petstore-for-all.sh
--
1577 | # START SCRIPT: bin/utils/test-fake-petstore-for-all.sh
1578 | ERROR: Failed to run 'ada' generator. The command was:
1579 | java -jar ./modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g ada -o /tmp/openapi-generator-test-fake-petstore/2.0/ada
1580 | ERROR: The output of the command was:
1581 | [main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
1582 | [main] WARN  o.o.c.ignore.CodegenIgnoreProcessor - Output directory does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated.
1583 | [main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: ada (client)
1584 | [main] INFO  o.o.codegen.DefaultGenerator - Generator 'ada' is considered stable.
1585 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/json)
1586 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/json)
1587 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1588 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1589 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1590 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1591 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1592 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1593 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1594 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1595 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1596 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1597 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1598 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1599 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1600 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1601 | [main] WARN  o.o.codegen.utils.ModelUtils - Multiple schemas found in the OAS 'content' section, returning only the first one (application/xml)
1602 | Exception in thread "main" java.lang.IllegalArgumentException: folder may not be null.
1603 | at org.openapitools.codegen.api.TemplateDefinition.<init>(TemplateDefinition.java:43)
1604 | at org.openapitools.codegen.SupportingFile.<init>(SupportingFile.java:41)
1605 | at org.openapitools.codegen.languages.AdaCodegen.processOpts(AdaCodegen.java:132)
1606 | at org.openapitools.codegen.DefaultGenerator.configureGeneratorProperties(DefaultGenerator.java:250)
1607 | at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:854)
1608 | at org.openapitools.codegen.cmd.Generate.execute(Generate.java:432)
1609 | at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
1610 | at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)


@jimschubert
Copy link
Member Author

@wing328 I've fixed those if you want to review.

@dtiller
Copy link
Contributor

dtiller commented Sep 9, 2020

This is a fantastic feature!

@krishtekula
Copy link

This is definitely cool feature, which will save lot of time and flexibility for users, without having to add custom generators.

@jimschubert jimschubert merged commit bf0bd29 into master Sep 12, 2020
@jimschubert jimschubert deleted the user-custom-added-templates branch September 12, 2020 22:10
@mathewab mathewab mentioned this pull request Apr 27, 2024
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants