Skip to content

Commit

Permalink
fix leaky abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
inponomarev committed Aug 22, 2024
1 parent e5a8bce commit fd4e452
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
8 changes: 1 addition & 7 deletions src/main/java/ru/curs/hurdygurdy/APIExtractor.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
34 changes: 20 additions & 14 deletions src/main/java/ru/curs/hurdygurdy/JavaAPIExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +36,10 @@ public JavaAPIExtractor(TypeDefiner<TypeSpec> typeDefiner,
GeneratorParams params) {
super(typeDefiner, params,
TypeSpec::interfaceBuilder,
TypeSpec.Builder::build);
b -> {
b.addModifiers(Modifier.PUBLIC);
return b.build();
});
}


Expand All @@ -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()
Expand All @@ -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())
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fd4e452

Please sign in to comment.