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

[JAVA] new Feature interface: Documentation Provider and Annotation Library #11258

Merged

Conversation

cachescrubber
Copy link
Contributor

@cachescrubber cachescrubber commented Jan 8, 2022

This PR introduces two new additional properties for all Java Based CodegenConfigs (extending AbstractJavaCodegen). The Properties are defined by a new Features interface, org.openapitools.codegen.languages.features.DocumentationProviderFeatures.

Synopsis

--additional-properties documentationProvider=springdoc,annotationLibrary=swagger2

When a documentation provider and annotation library are selected, a boolean property is added to the codegen model. For example, when SpringDoc is the selected as a documentation provider, the following properties are available in the mustache templates.

documentationProvider: springdoc
springDocDocumentationProvider: true
annotationLibrary: swagger2
swagger2AnnotationLibrary: true

The Documentation Provider and Annotation Libraries are defined in a dedicated Enum each.

My main goal is to define the property names used in the templates in a central location and to avoid having a dozen
of different additional property names and semantics in the various Java based CodegenConfigs.

The SpringCodegen serves as a demo of how to integrate into other Java based CodegenConfigs.

Naming is hard. The propertyNames and Annotation Values where not easy. Specially
Swagger1 and Swagger2 are based on the version numbers of their release artifacts, not using the target OAS specifiction version. Anyway, since the are defined in the Enums they are pretty easy to change in the current stage. Feel free to come up with suggestions and an alternative nameing.

Relates To

#8801
#9775
#11181

PR checklist

  • [ x] Read the contribution guidelines.
  • [ x] 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.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    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.
  • [x ] File the PR against the correct branch: master (5.3.0), 6.0.x
  • [ x] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@cachescrubber
Copy link
Contributor Author

@wing328 @welshm
@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10)

@cachescrubber
Copy link
Contributor Author

Docs and samples not updated yet - I'll wait for potential feedback and feasibility check.

@welshm
Copy link
Contributor

welshm commented Jan 8, 2022

Docs and samples not updated yet - I'll wait for potential feedback and feasibility check.

I'll try to take a look at this later today (EST)

@cachescrubber
Copy link
Contributor Author

cachescrubber commented Jan 8, 2022

DocumentationProvider

Cli Option Description Property Name Preferred Annotation Library Supported Annotation Libraries
none Do not publish an OpenAPI specification. withoutDocumentationProvider none none, swagger1, swagger2, microprofile
source Publish the original input OpenAPI specification. sourceDocumentationProvider none none, swagger1, swagger2, microprofile
swagger1 Generate a OpenAPI 2 (fka Swagger RESTful API Documentation Specification) specification using Swagger-Core 1.x. swagger1DocumentationProvider swagger1 swagger1
swagger2 Generate an OpenAPI 3 specification using Swagger-Core 2.x. swagger2DocumentationProvider swagger2 swagger2
springfox Generate a OpenAPI 2 (fka Swagger RESTful API Documentation Specification) specification using SpringFox 2.x. springFoxDocumentationProvider swagger1 swagger1
springdoc Generate an OpenAPI 3 specification using SpringDoc. springDocDocumentationProvider swagger2 swagger2

AnnotationLibrary

Cli Option Description Property Name
none Do not annotate Model and Api with complementary annotations. withoutAnnotationLibrary
swagger1 Annotate Model and Api using the Swagger Annotations 1.x library. swagger1AnnotationLibrary
swagger2 Annotate Model and Api using the Swagger Annotations 2.x library. swagger2AnnotationLibrary
microprofile Annotate Model and Api using the Microprofile annotations. microprofileAnnotationLibrary

@cachescrubber
Copy link
Contributor Author

Output of config-help -g spring

       documentationProvider
            Select the OpenAPI documentation provider. (Default: springdoc)
                none - Do not publish an OpenAPI specification.
                source - Publish the original input OpenAPI specification.
                springfox - Generate a Swagger 2 specification using SpringFox 2.x.
                springdoc - Generate an OpenAPI 3 specification using SpringDoc.

        annotationLibrary
            Select the complementary documentation annotation library. (Default: auto)
                none - Do not annotate Model and Api with complementary annotations.
                swagger1 - Annotate Model and Api using the Swagger Annotations 1.x library.
                swagger2 - Annotate Model and Api using the Swagger Annotations 2.x library.

@cachescrubber cachescrubber force-pushed the feature/documentation_provider_pr branch from 0320e69 to 4aaec1d Compare January 9, 2022 09:15
@@ -63,13 +63,13 @@ public class OpenAPI2SpringBoot implements CommandLineRunner {
.allowedMethods("*")
.allowedHeaders("Content-Type");
}*/
{{^useSpringfox}}
{{^springFoxDocumentationProvider}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right check? Or is the resource handler needed for specific non-Springfox providers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just replaced the template variable name - not the logic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... I suspect this will cause issues with some configurations but we can find out later

Comment on lines 62 to 73
NONE("withoutDocumentationProvider", "Do not publish an OpenAPI specification.",
AnnotationLibrary.NONE, AnnotationLibrary.values()),

SOURCE("sourceDocumentationProvider", "Publish the original input OpenAPI specification.",
AnnotationLibrary.NONE, AnnotationLibrary.values()),

SWAGGER1("swagger1DocumentationProvider", "Generate a Swagger 2 specification using Swagger-Core 1.x.",
AnnotationLibrary.SWAGGER1, AnnotationLibrary.SWAGGER1),

SWAGGER2("swagger2DocumentationProvider", "Generate an OpenAPI 3 specification using Swagger-Core 2.x.",
AnnotationLibrary.SWAGGER2, AnnotationLibrary.SWAGGER2),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the plan to add support for these providers before submitting this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - at least not in the spring generator. General Idea ist that other generators should use the same cli options and template variables to make the project easier to maintain in the long run.

If it is decided to make it spring generator only, I would probably remove those Enum values.

# Conflicts:
#	modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java
#	modules/openapi-generator/src/main/resources/JavaSpring/api.mustache
#	modules/openapi-generator/src/main/resources/JavaSpring/apiController.mustache
#	modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache
@cachescrubber
Copy link
Contributor Author

I merged in master and resolved conflicts after #11229.

This time I actually changed the sample configs and generated all samples.

All issues detected by the samples builds are resolved.

New: @ParameterObject support (SpringDoc feature to support spring-data Pageable).

@wing328
Copy link
Member

wing328 commented Jan 19, 2022

diff --git a/docs/generators/java-camel.md b/docs/generators/java-camel.md
index a55d98db..4c8768c9 100644
--- a/docs/generators/java-camel.md
+++ b/docs/generators/java-camel.md
@@ -20,6 +20,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
 |additionalEnumTypeAnnotations|Additional annotations for enum type(class level annotations)| |null|
 |additionalModelTypeAnnotations|Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)| |null|
 |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
+|annotationLibrary|Select the complementary documentation annotation library.|<dl><dt>**none**</dt><dd>Do not annotate Model and Api with complementary annotations.</dd><dt>**swagger1**</dt><dd>Annotate Model and Api using the Swagger Annotations 1.x library.</dd><dt>**swagger2**</dt><dd>Annotate Model and Api using the Swagger Annotations 2.x library.</dd></dl>|swagger2|
 |apiFirst|Generate the API from the OAI spec at server compile time (API first approach)| |false|
 |apiPackage|package for generated api classes| |org.openapitools.api|
 |artifactDescription|artifact description in generated pom.xml| |OpenAPI Java|
@@ -47,6 +48,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
 |disableHtmlEscaping|Disable HTML escaping of JSON strings when using gson (needed to avoid problems with byte[] fields)| |false|
 |disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|
 |discriminatorCaseSensitive|Whether the discriminator value lookup should be case-sensitive or not. This option only works for Java API client| |true|
+|documentationProvider|Select the OpenAPI documentation provider.|<dl><dt>**none**</dt><dd>Do not publish an OpenAPI specification.</dd><dt>**source**</dt><dd>Publish the original input OpenAPI specification.</dd><dt>**springfox**</dt><dd>Generate a OpenAPI 2 (fka Swagger RESTful API Documentation Specification) specification using SpringFox 2.x.</dd><dt>**springdoc**</dt><dd>Generate an OpenAPI 3 specification using SpringDoc.</dd></dl>|springdoc|

as reported by https://github.com/OpenAPITools/openapi-generator/runs/4866174156?check_suite_focus=true

Can you please repeat step 3 to have the doc updated? Thanks.

@cachescrubber
Copy link
Contributor Author

@wing328 I run export_docs_generators.sh and pushed the changes.

@wing328
Copy link
Member

wing328 commented Jan 22, 2022

@cachescrubber thanks again for the PR, which has been merged.

When you've time next week, can you please PM me via Slack? https://join.slack.com/t/openapi-generator/shared_invite/enQtNzAyNDMyOTU0OTE1LTY5ZDBiNDI5NzI5ZjQ1Y2E5OWVjMjZkYzY1ZGM2MWQ4YWFjMzcyNDY5MGI4NjQxNDBiMTlmZTc5NjY2ZTQ5MGM

@cachescrubber cachescrubber deleted the feature/documentation_provider_pr branch January 22, 2022 10:44
@cdprete
Copy link

cdprete commented Jan 30, 2022

Hi.
Is this implemented in the latest version (5.3.1)?

I'm trying to set, from Maven, annotationLibrary to none as documented at https://openapi-generator.tech/docs/generators/spring, but Swagger annotations are still added to the generated models.

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