OpenApi3: Is it possible to annotate a schema without creating a class?

Viewed 41

I'm currently trying to convert all my annotations from swagger 1 to swagger 2 and can't figure out how to annotate responses that use JSONObject.class. I'm using JAXRS-resteasy for my stack

Here is the resource that I am trying to annotate:

...
    @GET
    @Path("/isServerUp")
    @SubscriberAllowed({"active", "standby"})
    @Produces("application/json; charset=UTF-8")
    @Operation(
        summary = "Is Server Up?",
        description = "Returns a boolean representing whether the server is up",
        responses = {
            @ApiResponse(
                responseCode = "200",
                description = "Success",
                content = @Content(schema = @Schema(implementation = IsServerUpResponse200.class))
            )
        },
        tags = "Admin"
    )
    public JSONObject isServerUp() throws RESTException {
        Boolean isServerUp = false;
        isServerUp = Server.instance().isServerUp();
        JSONObject result = new JSONObject();
        result.put("isServerUp", isServerUp);
        return result;
    }
}

class IsServerUpResponse200 {
    @Schema(description = "True if Server is up")
    public Boolean isServerUp;
}

Is there anyway I can do this without having to create the IsServerUpResponse200 class? I would love to just use an annotation instead of having to create these sub-classes.

Ultimately, I want the openapi.yaml to look like this:

  /V1/Admin/isServerUp:
    get:
      tags:
      - Admin
      summary: Is Server Up?
      description: Returns a boolean representing whether the server is up.
      operationId: isServerUp
      responses:
        "200":
          description: Success
          content:
            application/json; charset=UTF-8:
              schema:
                 type: object
                 description: "True if Server is up"
                 properties:
                   isServerUp:
                     type: boolean

Edit 1:

I sort of found a way around it that is not very elegant, but gets the job done.

Here is my updated code:

...
    @GET
    @Path("/isServerUp")
    @SubscriberAllowed({"active", "standby"})
    @Produces("application/json; charset=UTF-8")
    @Operation(
        summary = "Is Server Up?",
        description = "Returns a boolean representing whether the server is up",
        responses = {
            @ApiResponse(
                responseCode = "200",
                description = "Success",
                content = @Content(schema = @Schema(
                    example = "{\n\"isServerUp\": true\n}",
                    type = "boolean",
                    description = "True if Server is up"
                ))
            )
        },
        tags = "Admin"
    )
    public JSONObject isServerUp() throws RESTException {
        Boolean isServerUp = false;
        isServerUp = Server.instance().isServerUp();
        JSONObject result = new JSONObject();
        result.put("isServerUp", isServerUp);
        return result;
    }
}
0 Answers
Related