Skip to content

Commit

Permalink
Fix single schema issue
Browse files Browse the repository at this point in the history
  • Loading branch information
SiBorea committed Dec 20, 2024
1 parent 81f82c3 commit b2444bc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {

testRuntimeOnly("io.swagger:swagger-annotations:1.6.13")
testRuntimeOnly("io.swagger.core.v3:swagger-annotations:2.2.20")
testRuntimeOnly("jakarta.ws.rs:jakarta.ws.rs-api:3.1.0")

testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
new JavaIsoVisitor<ExecutionContext>() {
private final AnnotationMatcher annotationMatcher = new AnnotationMatcher(FQN_SWAGGER_DEFINITION);

@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) {
cu = super.visitCompilationUnit(cu, executionContext);
return cu;
}

@Override
public J.@Nullable Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
J.Annotation ann = super.visitAnnotation(annotation, ctx);
Expand All @@ -66,12 +72,16 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
String servers = "";
if (basePath != null && host != null && schemes != null) {
tpl.append("servers = {\n");
for (Expression scheme : ((J.NewArray) schemes).getInitializer()) {
if (!servers.isEmpty()) {
servers += ",\n";
if (schemes instanceof J.FieldAccess) {
servers += "@Server(url = \"" + ((J.FieldAccess) schemes).getSimpleName().toLowerCase() + "://" + host + basePath + "\")";
} else if (schemes instanceof J.NewArray) {
for (Expression scheme : ((J.NewArray) schemes).getInitializer()) {
if (!servers.isEmpty()) {
servers += ",\n";
}
String schemeName = ((J.FieldAccess) scheme).getSimpleName().toLowerCase();
servers += "@Server(url = \"" + schemeName + "://" + host + basePath + "\")";
}
String schemeName = ((J.FieldAccess) scheme).getSimpleName().toLowerCase();
servers += "@Server(url = \"" + schemeName + "://" + host + basePath + "\")";
}
servers += "\n}";
parts.add(servers);
Expand All @@ -91,7 +101,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

ann = JavaTemplate.builder(tpl.toString())
.imports(FQN_OPENAPI_DEFINITION, FQN_SERVER)
.javaParser(JavaParser.fromJavaVersion().dependsOn(FQN_OPENAPI_DEFINITION, FQN_SERVER))
.javaParser(JavaParser.fromJavaVersion().classpath("swagger-annotations"))
.build()
.apply(updateCursor(ann), ann.getCoordinates().replace(), tplArgs.toArray());
maybeRemoveImport(FQN_SWAGGER_DEFINITION);
Expand All @@ -100,6 +110,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
ann = maybeAutoFormat(annotation, ann, ctx);
}

doAfterVisit(new RemoveUnusedImports().getVisitor());
return ann;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SwaggerToOpenAPITest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipeFromResources("org.openrewrite.openapi.swagger.SwaggerToOpenAPI")
.parser(JavaParser.fromJavaVersion().classpath("swagger-annotations-1.+", "swagger-annotations-2.+"));
.parser(JavaParser.fromJavaVersion().classpath("swagger-annotations-1.+", "swagger-annotations-2.+", "rs-api"));
}

@Test
Expand Down Expand Up @@ -132,9 +132,8 @@ class Example {
}

@Test
void migrateSwaggerDefinitionsToOpenAPIDefinition() {
void migrateSwaggerDefinitionsToOpenAPIDefinitionSingleSchema() {
rewriteRun(
recipeSpec -> recipeSpec.afterTypeValidationOptions(TypeValidation.none()),
//language=java
java(
"""
Expand All @@ -148,16 +147,54 @@ void migrateSwaggerDefinitionsToOpenAPIDefinition() {
info = @Info(title = "Example", version = "V1.0"),
consumes = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML },
produces = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML },
schemes = { SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS })
schemes = SwaggerDefinition.Scheme.HTTPS
)
class Example {
}
""",
"""
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.servers.Server;
@OpenAPIDefinition(
servers = {
@Server(url = "https://example.com/api")
},
info = @Info(title = "Example", version = "V1.0")
)
class Example {
}
"""
));
}

@Test
void migrateSwaggerDefinitionsToOpenAPIDefinitionMultipleSchema() {
rewriteRun(
// recipeSpec -> recipeSpec.afterTypeValidationOptions(TypeValidation.none()),
//language=java
java(
"""
import io.swagger.annotations.Info;
import io.swagger.annotations.SwaggerDefinition;
import jakarta.ws.rs.core.MediaType;
@SwaggerDefinition(
basePath = "/api",
host="example.com",
info = @Info(title = "Example", version = "V1.0"),
consumes = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML },
produces = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML },
schemes = { SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS })
class Example {
}
""",
"""
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.servers.Server;
@OpenAPIDefinition(
servers = {
@Server(url = "http://example.com/api"),
Expand Down

0 comments on commit b2444bc

Please sign in to comment.