OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
22.01k stars 6.6k forks source link

[REQ] Is it possible to use swagger 3 annotation in place of swagger 2 for java? #6108

Open TehBakker opened 4 years ago

TehBakker commented 4 years ago

Is your feature request related to a problem? Please describe.

I've migrated my API from swagger codeGen and swagger2 to openApi codeGen and openApi. I used to expose my paths and json spec to customer through springfox, and I want to update that as to expose an openApi spec file. Following the guide for migration found https://springdoc.org/migrating-from-springfox.html I see the annotation used by openApiCode are still the "swagger2" one

Describe the solution you'd like

Is there a way to configure that in my pom.xml as to use the newer annotation ? Is there anyway to configure that ?

Replace swagger 2 annotations with swagger 3 annotations (it is already included with springdoc-openapi-ui dependency). Package for swagger 3 annotations is io.swagger.v3.oas.annotations.

@ApiParam -> @Parameter @ApiOperation -> @Operation @Api -> @Tag @ApiImplicitParams -> @Parameters @ApiImplicitParam -> @Parameter @ApiIgnore -> @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden @ApiModel -> @Schema @ApiModelProperty -> @Schema

InfoSec812 commented 4 years ago

This is related to the work being done in #5803 and we should probably address this at a higher level than just the Spring generator to make it usable for all Java generators. @videki and others have already started work on the updated templates, but the main bottleneck is that the AbstractJavaCodegen uses the older annotations. I guess that my question becomes "Do we need to maintain support for the older Swagger 2 annotations or should we move completely to Swagger3/OAS?"

PieterJanMindCapture commented 2 years ago

I just tried this, it's available from

                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <version>5.3.1</version>

and you must use configOptions:

true true false

this will then generate the OAS3 annotations from:

io.swagger.core.v3 swagger-annotations 2.1.x
    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-models</artifactId>
        <version>2.1.x</version>
    </dependency>

and you can remove the Springfox libraries annotations (OAS3).

    <!--            <groupId>io.springfox</groupId>-->
    <!--            <artifactId>springfox-swagger2</artifactId>-->
    <!--            <version>${springfox.version}</version>-->
    <!--        </dependency>-->

however, there are some issues: the generated code will still generate the OAS2 import statements, even though they are unused. (auto-import clean from IDE fixes this on the fly)

However, a breaking issue with that the OAS3 annotation: @Hidden is generated on the parameters, but this annotation is not applicable to the parameter scope. and so causes a compile issue.

perhaps 5.4.0 fixes this?

mprachid commented 2 years ago

@PieterJanMindCapture what are the configOptions to set please ?

PieterJanMindCapture commented 2 years ago

generator options are here: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md

configOptions

in generator options: set: configHelp to true and run maven, it will print out all options for your version of the plugin, instead of generating files.

For "java" generator options are here, and specifically for language/generatorName= "spring" the configOptions are also documented here: https://openapi-generator.tech/docs/generators/spring/

eguller commented 2 years ago

for springboot is is possible with 5.3.1 version

<configOptions>
<library>spring-boot</library>
    <!-- all other config options -->
    <!-- following is added in v5.3.1 and import oas3 packages instead of swagger2.
    <oas3>true</oas3>
</configOptions>

see related issue https://github.com/OpenAPITools/openapi-generator/pull/9775

tofi86 commented 2 years ago

@eguller thanks for the pointer!

However, with the settings mentioned in your example and the ones in the MR #9775

                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/v1/openapi.yaml</inputSpec>

                            <generatorName>spring</generatorName>

                            <configOptions>
                                <library>spring-boot</library>
                                <oas3>true</oas3>
                                <useSpringController>true</useSpringController>
                                <useSpringfox>false</useSpringfox>
                            </configOptions>
                        </configuration>

Some models are generated with v3 annotations AND v2 annotations, for which the import fails:

...
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
...
import io.swagger.v3.oas.annotations.media.Schema;

Also, these imports are not used anywhere.

Any other configuration ideas to overcome this?

Or is this a bug from the oas3 feature? (cc @welshm)

Thanks! /Tobias

welshm commented 2 years ago

Hi @tofi86 ! So I did accidentally break some of the annotations for some of the mustaches I missed.

However, I think these have since been fixed by some wonderful work from @cachescrubber in https://github.com/OpenAPITools/openapi-generator/pull/11181 and two other improvements after that.

Hopefully that will be in a release soon :crossed_fingers:

tofi86 commented 2 years ago

Hi @welshm, well then we‘re looking forward to the next release! 👍

SamaNaresh commented 2 years ago
org.openapitools
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>5.3.1</version>

I don’t see the version '5.3.1' anywhere. Can you please explain how I can get 5.4.0 version. as you mentioned 5.3.1 have some issues

cachescrubber commented 2 years ago

@SamaNaresh 5.4.0 has been officially release and is available from the usual distribution points.

PieterJanMindCapture commented 2 years ago

It works. Thanks

PieterJanMindCapture commented 2 years ago

actually... there's still an issue with the generation of:

@Parameter(hidden = true) final ServerWebExchange exchange

and that is that: you can't assume that the project is running in a Reactor web context.

The default in spring is : servlet web context, if both servlet context (eg the so called spring-mvc) and a reactive libraries are found on the classpath it will default to picking Servlet web context.

One can have a project that is mostly rest-endpoints in servlet spring-mvc/boot web context, and have some select controllers return a webflux react Mono/Flux response.

However, in the default web servlet context, the bean ServerWebExchange does not exist. I've tried mocking it as well as creating a no-op bean, since, in the servlet context, this is completely unused. The project will not start without attempting to inject this bean, so I'm having to comment this out in the generated code.

Now, I've also noticed that the generated response is: produces = { "application/json" } And I think this should be: produces = { "text/event-stream"}

instead to confirm with the spec. However, even setting text/event-stream in the yaml/json spec file, the generator will still generate: produces = { "text/event-stream", "application/json" }

with both present, and that is because in OAS3, it is not possible to override the "default" content-type anymore.

cachescrubber commented 2 years ago

The media-type issue is fixed with https://github.com/OpenAPITools/openapi-generator/pull/11526

robinjhector commented 2 years ago

Is there any way of making the <generatorName>java</generatorName> with configOptions -> library=resttemplate generate annotations using swagger v3 annotations?

If I use <generatorName>spring</generatorName> and configOptions -> oas3=true it works great, but I cannot get the java generator to do the same. Any idea why?

welshm commented 2 years ago

Is there any way of making the <generatorName>java</generatorName> with configOptions -> library=resttemplate generate annotations using swagger v3 annotations?

If I use <generatorName>spring</generatorName> and configOptions -> oas3=true it works great, but I cannot get the java generator to do the same. Any idea why?

The option is currently only available for the spring generator unfortunately. You can open a feature request or contribute to creating it for the Java client :)

merrillgh commented 1 year ago

Is there any place to support OpenAPI 3+ in CXF?

aabdelli03 commented 8 months ago

I have the same issue with swagger codegen 3.0.34 , i did change swagger-annotations to io.swagger.codegen.v3 but still have a probleme with generated Apis. here is an example @ApiOperation(value = "Create element t", nickname = "createElementt", notes = "", response = IdDto.class, tags={ "element-controller", }) @ApiResponses(value = { @ApiResponse(code = 200, message = "the request was processed successfully", response = IdDto.class), @ApiResponse(code = 400, message = "client side error", response = ErrorDto.class), @ApiResponse(code = 401, message = "unauthorized call", response = ErrorDto.class), @ApiResponse(code = 406, message = "not acceptable call", response = ErrorDto.class), @ApiResponse(code = 500, message = "server side error", response = ErrorDto.class) }) @RequestMapping(value = "/technical-products", produces = { "application/v1+json; charset=utf-8" }, consumes = { "application/v1+json; charset=utf-8" }, method = RequestMethod.POST) ResponseEntity createElementt(@Parameter(in = ParameterIn.HEADER, description = "" ,required=true,schema=@Schema()) @RequestHeader(value="Authorization", required=true) String authorization, @Parameter(in = ParameterIn.DEFAULT, description = "", required=true, schema=@Schema()) @Valid @RequestBody ElementCreatFullDto body, @Parameter(in = ParameterIn.HEADER, description = "" ,schema=@Schema())")) throws ApiException;

all the annotations are not found either with imports i don't find them