简体   繁体   English

OpenAPI / Swagger-UI注释和@BeanParam

[英]OpenAPI/Swagger-UI Annotations & @BeanParam

I am developing a set of JAX-RS services using Quarkus. 我正在使用Quarkus开发一组JAX-RS服务。 I'm also annotating them with the OpenAPI/Swagger-UI annotations for easy generation of API documentation. 我还使用OpenAPI / Swagger-UI注释对它们进行注释,以简化API文档的生成。 I'm able to annotate my GET services like so... 我可以像这样注释我的GET服务...

    @Path("/{communityIdentifier}")
    @GET
    @Operation(summary = "Get community details", description = "Get detailed information about a community")
    @APIResponses({
            @APIResponse(responseCode = "200", description = "The community details", content = @Content(schema = @Schema(ref = "community"))),
            @APIResponse(name = "401", responseCode = "401", description = "Authentication required"),
            @APIResponse(name = "403", responseCode = "403", description = "Permission denied - you do not have access to access this resource", content = @Content(schema = @Schema(ref = "baseError"))),
            @APIResponse(name = "404", responseCode = "404", description = "Resource not found", content = @Content(schema = @Schema(ref = "baseError"))),
            @APIResponse(name = "500", responseCode = "500", description = "Internal service error", content = @Content(schema = @Schema(ref = "baseError"))) })
    @Timed(name = "getCommunityTimer")
    @Counted(name = "getCommunityCount")
    public Response getCommunity(
            @PathParam("securityRealm") @Parameter(name = "securityRealm", description = "The security realm name", required = true) String securityRealmName,
            @PathParam("communityIdentifier") @Parameter(name = "communityIdentifier", description = "The community identifier", required = true) String communityIdentifier) {
 // Stuff
}

When I access my Swagger UI endpoint, I see nicely documented entries for this service, including information on the securityRealm and communityIdentifier parameters. 当我访问Swagger UI端点时,我看到了该服务的详细记录条目,包括关于securityRealmcommunityIdentifier参数的信息。 Now I am trying to create POST and PUT methods in a similar manner and am running into an issue. 现在,我试图以类似的方式创建POSTPUT方法,并且遇到了问题。

Since my PUT / POST requests contain a number of form parameters, I'm encapsulating them into an object and annotating the method with @BeanParam . 由于我的PUT / POST请求包含许多表单参数,因此我将它们封装到一个对象中,并使用@BeanParam对该方法进行@BeanParam My form object looks like this: 我的表单对象如下所示:

public class CommunityRequestForm extends AbstractRequestForm {

    private static final long serialVersionUID = 6007695645505404656L;

    @FormParam("description")
    private String description = null;

    @FormParam("name")
    private String name = null;

    public String getDescription() {
        return description;
    }

    public String getName() {
        return name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setName(String name) {
        this.name = name;
    }

}

My POST method looks like this: 我的POST方法如下:

    @Path("/")
    @POST
    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
    @Operation(summary = "Create a community", description = "Creates a new community within a security realm")
    @APIResponses({
            @APIResponse(responseCode = "201", description = "The community details", content = @Content(schema = @Schema(ref = "community"))),
            @APIResponse(name = "401", responseCode = "401", description = "Authentication required"),
            @APIResponse(name = "403", responseCode = "403", description = "Permission denied - you do not have access to access this resource", content = @Content(schema = @Schema(ref = "baseError"))),
            @APIResponse(name = "404", responseCode = "404", description = "Resource not found", content = @Content(schema = @Schema(ref = "baseError"))),
            @APIResponse(name = "500", responseCode = "500", description = "Internal service error", content = @Content(schema = @Schema(ref = "baseError"))) })
    public Response createCommunity(
            @PathParam("securityRealm") @Parameter(name = "securityRealm", description = "The security realm name", required = true) String securityRealmName,
            @BeanParam CommunityRequestForm communityForm) {
  // Stuff
}

So far, so good. 到现在为止还挺好。 However, I have not been able to figure out how to annotate the name and description parameters so they are documented by OpenAPI/Swagger-UI. 但是,我无法弄清楚如何注释namedescription参数,因此它们由OpenAPI / Swagger-UI记录。 I tried changing the parameter annotations and definition to look like this: 我尝试将参数注释和定义更改为如下所示:

@FormParam("description")
@Parameter(name = "description", required = true, style = ParameterStyle.FORM)
private String description = null;

That did nothing. 那什么也没做。 I tried changing my method signature to look like this: 我尝试将方法签名更改为如下形式:

public Response createCommunity(
            @PathParam("securityRealm") @Parameter(name = "securityRealm", description = "The security realm name", required = true) String securityRealmName,
            @Parameter(style = ParameterStyle.FORM) @BeanParam CommunityRequestForm communityForm)

Still nothing. 依然没有。 My question is, is there a way to get the OpenAPI/Swagger-UI annotations to play nicely and document a @BeanParam annotated object? 我的问题是,有没有一种方法可以使OpenAPI / Swagger-UI注释很好地播放并记录@BeanParam注释的对象? Or is this approach not supported at all? 还是根本不支持这种方法?

I know they improved all that in SmallRye OpenAPI 1.1.5 with this PR: https://github.com/smallrye/smallrye-open-api/pull/138 . 我知道他们通过此PR改进了SmallRye OpenAPI 1.1.5中的所有功能: https : //github.com/smallrye/smallrye-open-api/pull/138

Quarkus 0.22.0 still uses 1.1.3. Quarkus 0.22.0仍使用1.1.3。 We have updated to 1.1.8 in master so the soon to be released 0.23.0 will have the update. 我们已经更新了master中的1.1.8,因此即将发布的0.23.0将进行更新。

You can try with the Quarkus master branch (just build it with mvn clean install -DskipTests -DskipITs then change the version to 999-SNAPSHOT ). 您可以尝试使用Quarkus master分支(只需使用mvn clean install -DskipTests -DskipITs构建它,然后将版本更改为999-SNAPSHOT )。 Hopefully, things will be better. 希望情况会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM