Skip to content

Commit

Permalink
Implement and test ConvertApiResponseCodesToStrings
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Feb 10, 2024
1 parent 49d382c commit 38a5b13
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 44 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ dependencies {

testImplementation("org.junit.jupiter:junit-jupiter-engine:latest.release")

testRuntimeOnly("io.swagger:swagger-annotations:1.6.13")
testRuntimeOnly("io.swagger.core.v3:swagger-annotations:2.2.20")

testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@
package org.openrewrite.openapi.swagger;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

public class ConvertApiResponseCodesToStrings extends Recipe {

private static final AnnotationMatcher ANNOTATION_MATCHER = new AnnotationMatcher("@io.swagger.v3.oas.annotations.responses.ApiResponse");

@Override
public String getDisplayName() {
return "Convert API response codes to strings";
Expand All @@ -33,8 +43,40 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>(){
// TODO: implement
};
// https://docs.swagger.io/swagger-core/v1.5.0/apidocs/io/swagger/annotations/ApiResponse.html
// https://docs.swagger.io/swagger-core/v2.0.0/apidocs/io/swagger/v3/oas/annotations/responses/ApiResponse.html
return Preconditions.check(
new UsesType<>("io.swagger.v3.oas.annotations.responses.ApiResponse", true),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
J.Annotation an = super.visitAnnotation(annotation, ctx);
if (ANNOTATION_MATCHER.matches(an)) {
return an.withArguments(ListUtils.map(an.getArguments(), this::maybeReplaceResponseCodeTypeAndValue));
}
return an;
}

private Expression maybeReplaceResponseCodeTypeAndValue(Expression arg) {
if (arg instanceof J.Assignment) {
J.Assignment assignment = (J.Assignment) arg;
boolean matchesField = assignment.getVariable() instanceof J.Identifier &&
"responseCode".equals(((J.Identifier) assignment.getVariable()).getSimpleName());
boolean usesNumberLiteral = assignment.getAssignment() instanceof J.Literal &&
((J.Literal) assignment.getAssignment()).getValue() instanceof Number;
if (matchesField && usesNumberLiteral) {
J.Literal assignedLiteral = (J.Literal) assignment.getAssignment();
return assignment
.withType(JavaType.Primitive.String)
.withAssignment(assignedLiteral
.withValue(String.valueOf(assignedLiteral.getValue()))
.withValueSource("\"" + assignedLiteral.getValue() + "\"")
.withType(JavaType.Primitive.String));
}
}
return arg;
}
}
);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.openrewrite.openapi.swagger;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.*;

class ConvertApiResponseCodesToStringsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipeFromResources("org.openrewrite.openapi.MigrateApiResponsesToApiResponses")
.parser(JavaParser.fromJavaVersion().classpath("swagger-annotations-1.+", "swagger-annotations-2.+"));
}

@Test
@DocumentExample
void convertApiResponseCodesToStrings() {
rewriteRun(
//language=java
java(
"""
import io.swagger.annotations.ApiResponse;
class A {
@ApiResponse(code = 200, message = "OK")
void method() {}
}
""",
"""
import io.swagger.v3.oas.annotations.responses.ApiResponse;
class A {
@ApiResponse(responseCode = "200", description = "OK")
void method() {}
}
"""
)
);
}

@Test
void noChangeOnAlreadyConverted() {
rewriteRun(
//language=java
java(
"""
import io.swagger.v3.oas.annotations.responses.ApiResponse;
class A {
@ApiResponse(responseCode = "200", description = "OK")
void method() {}
}
"""
)
);
}
}

0 comments on commit 38a5b13

Please sign in to comment.