Skip to content

Commit

Permalink
Handle @ApiImplicitParam(dataTypeClass=..) (#22)
Browse files Browse the repository at this point in the history
* Handle @ApiImplicitParam(dataTypeClass=..)

* Replace `TypeValidation.none` with proper types with classpath entry

* Organize imports

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
SiBorea and timtebeek authored Dec 9, 2024
1 parent c11e0bc commit 2c1eb37
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 5 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencies {
implementation("org.openrewrite:rewrite-java")
implementation("org.openrewrite.recipe:rewrite-java-dependencies:$rewriteVersion")

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

testImplementation("org.openrewrite:rewrite-java-17")
testImplementation("org.openrewrite:rewrite-test")
testImplementation("org.openrewrite:rewrite-gradle")
Expand All @@ -19,7 +21,6 @@ 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
@@ -0,0 +1,90 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.openapi.swagger;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;

import java.util.ArrayList;
import java.util.List;

public class MigrateApiImplicitParamDataTypeClass extends Recipe {
private static final String FQN_SCHEMA = "io.swagger.v3.oas.annotations.media.Schema";

@Override
public String getDisplayName() {
return "Migrate `@ApiImplicitParam(dataTypeClass=Foo.class)` to `@Parameter(schema=@Schema(implementation=Foo.class))`";
}

@Override
public String getDescription() {
return "Migrate `@ApiImplicitParam(dataTypeClass=Foo.class)` to `@Parameter(schema=@Schema(implementation=Foo.class))`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
// This recipe is after ChangeType recipe
return Preconditions.check(
new UsesMethod<>("io.swagger.annotations.ApiImplicitParam dataTypeClass()", false),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
J.Annotation anno = super.visitAnnotation(annotation, ctx);

if (!new AnnotationMatcher("io.swagger.v3.oas.annotations.Parameter").matches(anno)) {
return anno;
}

StringBuilder tpl = new StringBuilder();
List<Expression> args = new ArrayList<>();
for (Expression exp : anno.getArguments()) {
if (!args.isEmpty()) {
tpl.append(", ");
}
if (isDataTypeClass(exp)) {
J.FieldAccess fieldAccess = (J.FieldAccess) ((J.Assignment) exp).getAssignment();
tpl.append("schema = @Schema(implementation = #{any()})");
args.add(fieldAccess);
} else {
tpl.append("#{any()}");
args.add(exp);
}
}
anno = JavaTemplate.builder(tpl.toString())
.imports(FQN_SCHEMA)
.javaParser(JavaParser.fromJavaVersion().classpath("swagger-annotations"))
.build()
.apply(updateCursor(anno), annotation.getCoordinates().replaceArguments(), args.toArray());
maybeAddImport(FQN_SCHEMA, false);
return maybeAutoFormat(annotation, anno, ctx, getCursor().getParentTreeCursor());
}

private boolean isDataTypeClass(Expression exp) {
return exp instanceof J.Assignment && ((J.Identifier) ((J.Assignment) exp).getVariable()).getSimpleName().equals("dataTypeClass");
}
}
);
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/swagger-2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ recipeList:
- org.openrewrite.java.RemoveAnnotationAttribute:
annotationType: io.swagger.v3.oas.annotations.Parameter
attributeName: allowMultiple
- org.openrewrite.openapi.swagger.MigrateApiImplicitParamDataTypeClass

---
type: specs.openrewrite.org/v1beta/recipe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ void shouldChangeSwaggerArtifacts() {
java(
"""
package example.org;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value="ApiModelExampleValue", description="ApiModelExampleDescription")
class Example {
@ApiModelProperty(value = "ApiModelPropertyExampleValue", position = 1)
Expand All @@ -60,9 +60,9 @@ class Example {
""",
"""
package example.org;
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(name="ApiModelExampleValue", description="ApiModelExampleDescription")
class Example {
@Schema(description = "ApiModelPropertyExampleValue")
Expand Down Expand Up @@ -130,4 +130,32 @@ class Example {
)
);
}

@Test
void migrateApiImplicitParamDataTypeClass() {
rewriteRun(
//language=java
java(
"""
import io.swagger.annotations.ApiImplicitParam;
class Example {
@ApiImplicitParam(name = "foo", value = "Foo object", required = true, dataTypeClass = Example.class)
public void create(Example foo) {
}
}
""",
"""
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
class Example {
@Parameter(name = "foo", description = "Foo object", required = true, schema = @Schema(implementation = Example.class))
public void create(Example foo) {
}
}
"""
)
);
}
}

0 comments on commit 2c1eb37

Please sign in to comment.