From fd4e452a965813bd6cc646d5419864f7c07a4dfc Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 22 Aug 2024 10:08:23 +0300 Subject: [PATCH] fix leaky abstraction --- .../java/ru/curs/hurdygurdy/APIExtractor.java | 8 +---- .../ru/curs/hurdygurdy/JavaAPIExtractor.java | 34 +++++++++++-------- ...oNotGenerateResponseParameter.approved.txt | 2 +- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/java/ru/curs/hurdygurdy/APIExtractor.java b/src/main/java/ru/curs/hurdygurdy/APIExtractor.java index f76500b..ef894bb 100644 --- a/src/main/java/ru/curs/hurdygurdy/APIExtractor.java +++ b/src/main/java/ru/curs/hurdygurdy/APIExtractor.java @@ -1,6 +1,5 @@ package ru.curs.hurdygurdy; -import com.squareup.javapoet.TypeSpec; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.PathItem; @@ -9,7 +8,6 @@ import io.swagger.v3.oas.models.media.MediaType; import io.swagger.v3.oas.models.parameters.Parameter; -import javax.lang.model.element.Modifier; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; @@ -70,11 +68,7 @@ private void generateClass(OpenAPI openAPI, Paths paths, String name, boolean re operationId = CaseUtils.pathToCamel(stringPathItemEntry.getKey()) + CaseUtils.snakeToCamel(operationEntry.getKey().name().toLowerCase(), true); } - B b = builder(typeName); - if ("Controller".equals(name) && b instanceof TypeSpec.Builder) { - ((TypeSpec.Builder) b).addModifiers(Modifier.PUBLIC); - } - buildMethod(openAPI, b, stringPathItemEntry, + buildMethod(openAPI, builder(typeName), stringPathItemEntry, operationEntry, operationId, responseParameter); } } diff --git a/src/main/java/ru/curs/hurdygurdy/JavaAPIExtractor.java b/src/main/java/ru/curs/hurdygurdy/JavaAPIExtractor.java index 979a301..7447ecd 100644 --- a/src/main/java/ru/curs/hurdygurdy/JavaAPIExtractor.java +++ b/src/main/java/ru/curs/hurdygurdy/JavaAPIExtractor.java @@ -23,7 +23,9 @@ import org.springframework.web.bind.annotation.RequestPart; import javax.lang.model.element.Modifier; + import jakarta.servlet.http.HttpServletResponse; + import java.util.Map; import java.util.Optional; import java.util.stream.Stream; @@ -34,7 +36,10 @@ public JavaAPIExtractor(TypeDefiner typeDefiner, GeneratorParams params) { super(typeDefiner, params, TypeSpec::interfaceBuilder, - TypeSpec.Builder::build); + b -> { + b.addModifiers(Modifier.PUBLIC); + return b.build(); + }); } @@ -56,16 +61,17 @@ void buildMethod(OpenAPI openAPI, TypeSpec.Builder classBuilder, .flatMap(c -> getContentTypes(c, openAPI, classBuilder)) .forEach(paramSpec -> methodBuilder.addParameter(ParameterSpec.builder( - paramSpec.typeName, - paramSpec.name) + paramSpec.typeName, + paramSpec.name) .addAnnotation(paramSpec.annotation).build()) ); getParameterStream(stringPathItemEntry.getValue(), operationEntry.getValue()) .filter(parameter -> "path".equalsIgnoreCase(parameter.getIn())) .forEach(parameter -> methodBuilder.addParameter(ParameterSpec.builder( - safeUnbox(typeDefiner.defineJavaType(parameter.getSchema(), openAPI, classBuilder, null)), - CaseUtils.snakeToCamel(parameter.getName())) + safeUnbox(typeDefiner.defineJavaType(parameter.getSchema(), + openAPI, classBuilder, null)), + CaseUtils.snakeToCamel(parameter.getName())) .addAnnotation( AnnotationSpec.builder(PathVariable.class) .addMember("name", "$S", parameter.getName()).build() @@ -75,8 +81,8 @@ void buildMethod(OpenAPI openAPI, TypeSpec.Builder classBuilder, .filter(parameter -> "query".equalsIgnoreCase(parameter.getIn())) .forEach(parameter -> { AnnotationSpec.Builder builder = AnnotationSpec.builder( - RequestParam.class - ).addMember("required", "$L", parameter.getRequired()) + RequestParam.class + ).addMember("required", "$L", parameter.getRequired()) .addMember("name", "$S", parameter.getName()); Optional.ofNullable(parameter.getSchema()) @@ -86,21 +92,21 @@ void buildMethod(OpenAPI openAPI, TypeSpec.Builder classBuilder, AnnotationSpec annotationSpec = builder.build(); methodBuilder.addParameter(ParameterSpec.builder( - safeBox(typeDefiner.defineJavaType(parameter.getSchema(), openAPI, - classBuilder, null)), - CaseUtils.snakeToCamel(parameter.getName())) + safeBox(typeDefiner.defineJavaType(parameter.getSchema(), openAPI, + classBuilder, null)), + CaseUtils.snakeToCamel(parameter.getName())) .addAnnotation(annotationSpec).build()); } ); getParameterStream(stringPathItemEntry.getValue(), operationEntry.getValue()) .filter(parameter -> "header".equalsIgnoreCase(parameter.getIn())) .forEach(parameter -> methodBuilder.addParameter(ParameterSpec.builder( - safeBox(typeDefiner.defineJavaType(parameter.getSchema(), openAPI, classBuilder, null)), - CaseUtils.kebabToCamel(parameter.getName())) + safeBox(typeDefiner.defineJavaType(parameter.getSchema(), openAPI, classBuilder, null)), + CaseUtils.kebabToCamel(parameter.getName())) .addAnnotation( AnnotationSpec.builder( - RequestHeader.class - ).addMember("required", "$L", parameter.getRequired()) + RequestHeader.class + ).addMember("required", "$L", parameter.getRequired()) .addMember("name", "$S", parameter.getName()).build() ).build())); if (generateResponseParameter) { diff --git a/src/test/java/ru/curs/hurdygurdy/CodegenTest.doNotGenerateResponseParameter.approved.txt b/src/test/java/ru/curs/hurdygurdy/CodegenTest.doNotGenerateResponseParameter.approved.txt index 0b7736b..4cadd4d 100644 --- a/src/test/java/ru/curs/hurdygurdy/CodegenTest.doNotGenerateResponseParameter.approved.txt +++ b/src/test/java/ru/curs/hurdygurdy/CodegenTest.doNotGenerateResponseParameter.approved.txt @@ -23,7 +23,7 @@ import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; -interface Api { +public interface Api { @GetMapping( value = "/menu", produces = "application/json"