openrewrite / rewrite-openapi

OpenRewrite recipes for OpenAPI.
Apache License 2.0
3 stars 7 forks source link

Cloud provides response type change recipe? #18

Open stevenniu9527 opened 2 days ago

stevenniu9527 commented 2 days ago

I have tried the existing recipe. Now the recipe MigrateApiOperationToOperation directly deletes the response without change.

- org.openrewrite.java.RemoveAnnotationAttribute:
      annotationType: io.swagger.v3.oas.annotations.Operation
      attributeName: response

How to Migrate from Swagger to OpenAPI, Get the replacement below. Especially reserve the reponse type

@ApiOperation(value = "acquireSyncLeader", notes = "acquireSyncLeader", response = String.class, httpMethod = "GET") i hope after migrate :

@Operation(summary = "acquireSyncLeader", description = "acquireSyncLeader", responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = String.class))),

timtebeek commented 1 day ago

hi @stevenniu9527 ; thanks for reporting here! Indeed right now the recipe does a simple removal https://github.com/openrewrite/rewrite-openapi/blob/178666d08dd2b8b577f974be9ccee79f179c6ef8/src/main/resources/META-INF/rewrite/swagger-2.yml#L128-L130

To go from the before to after scenario you've described we'd need to add a dedicated recipe that adds the new replacement responses attribute instead. That would likely be similar to this existing recipe, if you'd like an example to help you implement this: https://github.com/openrewrite/rewrite-openapi/blob/178666d08dd2b8b577f974be9ccee79f179c6ef8/src/main/java/org/openrewrite/openapi/swagger/ConvertApiResponseCodesToStrings.java#L50-L79

And perhaps good to check: are you certain that change is necessary? What's default without that additional property?

stevenniu9527 commented 1 day ago

I think the return type content = @Content(mediaType = "application/json", schema = @Schema(implementation = User.class)) is a necessary swagger parameter and is used to generate swagger interface documentation. like Example 2 in swagger doc:https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations

    @GET
    @Path("/{username}")
    @Operation(summary = "Get user by user name",
            responses = {
                    @ApiResponse(description = "The user",
                            content = @Content(mediaType = "application/json",
                                    schema = @Schema(implementation = User.class))),
                    @ApiResponse(responseCode = "400", description = "User not found")})
    public Response getUserByName(
            @Parameter(description = "The name that needs to be fetched. Use user1 for testing. ", required = true) @PathParam("username") String username)
            throws ApiException {
        User user = userData.findUserByName(username);
        if (null != user) {
            return Response.ok().entity(user).build();
        } else {
            throw new NotFoundException(404, "User not found");
        }
    }

The output for Example 2 would be:

/user/{username}:
    get:
      summary: Get user by user name
      operationId: getUserByName
      parameters:
      - name: username
        in: path
        description: 'The name that needs to be fetched. Use user1 for testing. '
        required: true
        schema:
          type: string
      responses:
        default:
          description: The user
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
        400:
          description: User not found
timtebeek commented 1 day ago

That's helpful info, thanks! Would you be willing and able to help create that recipe? We have some self-guided recipe authoring resources here to help you get started.