Skip to content

Commit

Permalink
Migrate @ApiParam(defaultValue) to `@Parameter(schema = @Schema(def…
Browse files Browse the repository at this point in the history
…aultValue))` (#28)

* Migrate @ApiParam(defaultValue)

* Update src/main/java/org/openrewrite/openapi/swagger/MigrateApiParamDefaultValue.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Extract constant for annotation matcher

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
4 people authored Jan 10, 2025
1 parent 2d88071 commit 03b3e32
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MigrateApiImplicitParam extends Recipe {

@Override
public String getDisplayName() {
return "Migrate `@ApiImplicitParam` to `@Parameter)`";
return "Migrate `@ApiImplicitParam` to `@Parameter`";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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 MigrateApiParamDefaultValue extends Recipe {
private static final String FQN_SCHEMA = "io.swagger.v3.oas.annotations.media.Schema";
private static final AnnotationMatcher PARAMETER_ANNOTATION_MATCHER = new AnnotationMatcher("io.swagger.v3.oas.annotations.Parameter");

@Override
public String getDisplayName() {
return "Migrate `@ApiParam(defaultValue)` to `@Parameter(schema)`";
}

@Override
public String getDescription() {
return "Migrate `@ApiParam(defaultValue)` to `@Parameter(schema = @Schema(defaultValue))`.";
}

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

if (!PARAMETER_ANNOTATION_MATCHER.matches(anno)) {
return anno;
}

StringBuilder tpl = new StringBuilder();
StringBuilder schemaTpl = new StringBuilder();
List<Expression> args = new ArrayList<>();
for (Expression exp : anno.getArguments()) {
if (isDefaultValue(exp)) {
Expression expression = ((J.Assignment) exp).getAssignment();
addSchema(schemaTpl, "defaultValue");
args.add(expression);
} else {
tpl.append("#{any()}, ");
args.add(exp);
}
}
if (tpl.toString().endsWith(", ")) {
tpl.delete(tpl.length() - 2, tpl.length());
}
if (schemaTpl.length() > 0) {
if (schemaTpl.toString().endsWith(", ")) {
schemaTpl.delete(schemaTpl.length() - 2, schemaTpl.length());
}
schemaTpl.append(")");
tpl.append(", ").append(schemaTpl);
}
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 void addSchema(StringBuilder tpl, String key) {
if (tpl.length() == 0) {
tpl.append("schema = @Schema(");
}
tpl.append(key).append(" = #{any()}, ");
}

private boolean isDefaultValue(Expression exp) {
return exp instanceof J.Assignment && ((J.Identifier) ((J.Assignment) exp).getVariable()).getSimpleName().equals("defaultValue");
}
}
);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/rewrite/swagger-2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ recipeList:
annotationType: io.swagger.v3.oas.annotations.Parameter
oldAttributeName: "value"
newAttributeName: "description"
- org.openrewrite.java.RemoveAnnotationAttribute:
annotationType: io.swagger.v3.oas.annotations.Parameter
attributeName: allowMultiple
- org.openrewrite.openapi.swagger.MigrateApiParamDefaultValue

---
type: specs.openrewrite.org/v1beta/recipe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,32 @@ public void create(Example foo) {
);
}

@Test
void migrateApiParam() {
rewriteRun(
//language=java
java(
"""
import io.swagger.annotations.ApiParam;
class Example {
@ApiParam(name = "foo", value = "Foo Object", required = false, defaultValue = "bar")
private Integer 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 = false, schema = @Schema(defaultValue = "bar"))
private Integer foo;
}
"""
)
);
}

@Test
void migrateSwaggerDefinitionsToOpenAPIDefinitionSingleSchema() {
rewriteRun(
Expand Down

0 comments on commit 03b3e32

Please sign in to comment.