Skip to content

Fix Jsonb Controller for modular projects without compiler plugin #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ public class WidgetController$Route implements WebRoutes {
});

}

}
```

Expand Down Expand Up @@ -190,7 +189,6 @@ public class WidgetController$Route implements Service {
private void _getAll(ServerRequest req, ServerResponse res) {
res.send(controller.getAll());
}

}
```

Expand Down Expand Up @@ -224,7 +222,6 @@ public class WidgetController$Route implements HttpService {
var result = controller.getAll();
res.send(result);
}

}
```

Expand Down Expand Up @@ -264,7 +261,6 @@ public class WidgetController$Route implements WebRoutes {
});

}

}
```

Expand Down Expand Up @@ -296,16 +292,15 @@ public class WidgetController$Route implements HttpService {
var pathParams = req.path().pathParameters();
int id = asInt(pathParams.first("id").get());
var result = controller.getById(id);
res.headers().contentType(io.helidon.common.http.HttpMediaType.APPLICATION_JSON);
widgetJsonType.toJson(result, res.outputStream());
res.headers().contentType(HttpMediaType.APPLICATION_JSON);
widgetJsonType.toJson(result, JsonOutput.of(res));
}

private void _getAll(ServerRequest req, ServerResponse res) {
var pathParams = req.path().pathParameters();
var result = controller.getAll();
res.headers().contentType(io.helidon.common.http.HttpMediaType.APPLICATION_JSON);
listWidgetJsonType.toJson(result, res.outputStream());
res.headers().contentType(HttpMediaType.APPLICATION_JSON);
listWidgetJsonType.toJson(result, JsonOutput.of(res));
}

}
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,19 @@
import io.avaje.http.generator.core.ClientPrism;
import io.avaje.http.generator.core.ControllerReader;
import io.avaje.http.generator.core.ImportPrism;
import io.avaje.http.generator.core.JsonBUtil;
import io.avaje.http.generator.core.ProcessingContext;

@SupportedAnnotationTypes({ClientPrism.PRISM_TYPE, ImportPrism.PRISM_TYPE})
public class ClientProcessor extends AbstractProcessor {

private final ComponentMetaData metaData = new ComponentMetaData();

private final boolean useJsonB;
private boolean useJsonB;

private SimpleComponentWriter componentWriter;

private boolean readModuleInfo;

public ClientProcessor() {
useJsonB = JsonBUtil.detectJsonb();
}

public ClientProcessor(boolean useJsonb) {
useJsonB = useJsonb;
}

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
Expand All @@ -52,8 +43,8 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
this.processingEnv = processingEnv;
ProcessingContext.init(processingEnv, new ClientPlatformAdapter(), false);

this.componentWriter = new SimpleComponentWriter(metaData);
useJsonB = ProcessingContext.useJsonb();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
@SupportedOptions({"useJavax", "useSingleton", "instrumentRequests","disableDirectWrites"})
public abstract class BaseProcessor extends AbstractProcessor {

protected boolean useJsonB;

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
Expand All @@ -28,6 +30,7 @@ public Set<String> getSupportedAnnotationTypes() {
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
ProcessingContext.init(processingEnv, providePlatformAdapter());
useJsonB = ProcessingContext.useJsonb();
}

/** Provide the platform specific adapter to use for Javalin, Helidon etc. */
Expand All @@ -44,7 +47,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

final Set<? extends Element> controllers =
round.getElementsAnnotatedWith(typeElement(ControllerPrism.PRISM_TYPE));
for (Element controller : controllers) {
for (final Element controller : controllers) {
writeAdapter(controller);
}

Expand All @@ -57,32 +60,32 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
private void readOpenApiDefinition(RoundEnvironment round) {
final Set<? extends Element> elements =
round.getElementsAnnotatedWith(typeElement(OpenAPIDefinitionPrism.PRISM_TYPE));
for (Element element : elements) {
for (final Element element : elements) {
doc().readApiDefinition(element);
}
}

private void readTagDefinitions(RoundEnvironment round) {
Set<? extends Element> elements =
round.getElementsAnnotatedWith(typeElement(TagPrism.PRISM_TYPE));
for (Element element : elements) {
for (final Element element : elements) {
doc().addTagDefinition(element);
}

elements = round.getElementsAnnotatedWith(typeElement(TagsPrism.PRISM_TYPE));
for (Element element : elements) {
for (final Element element : elements) {
doc().addTagsDefinition(element);
}
}

private void readSecuritySchemes(RoundEnvironment round) {
Set<? extends Element> elements = round.getElementsAnnotatedWith(typeElement(SecuritySchemePrism.PRISM_TYPE));
for (Element element : elements) {
for (final Element element : elements) {
doc().addSecurityScheme(element);
}

elements = round.getElementsAnnotatedWith(typeElement(SecuritySchemesPrism.PRISM_TYPE));
for (Element element : elements) {
for (final Element element : elements) {
doc().addSecuritySchemes(element);
}
}
Expand All @@ -93,11 +96,11 @@ private void writeOpenAPI() {

private void writeAdapter(Element controller) {
if (controller instanceof TypeElement) {
ControllerReader reader = new ControllerReader((TypeElement) controller);
final ControllerReader reader = new ControllerReader((TypeElement) controller);
reader.read(true);
try {
writeControllerAdapter(reader);
} catch (Throwable e) {
} catch (final Throwable e) {
e.printStackTrace();
logError(reader.beanType(), "Failed to write $Route class " + e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@
public class JsonBUtil {
private JsonBUtil() {}

/**
* Return true if avaje-jsonb is detected in the classpath.
*/
public static boolean detectJsonb() {
try {
Class.forName("io.avaje.jsonb.Jsonb");
return true;
} catch (final ClassNotFoundException e) {
return false;
}
}

public static Map<String, UType> jsonTypes(ControllerReader reader) {

final Map<String, UType> jsonTypes = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private static final class Ctx {
private final String diAnnotation;
private final boolean instrumentAllMethods;
private final boolean disableDirectWrites;
private final boolean useJsonb;

Ctx(ProcessingEnvironment env, PlatformAdapter adapter, boolean generateOpenAPI) {
readAdapter = adapter;
Expand All @@ -65,7 +66,7 @@ private static final class Ctx {
useComponent = elementUtils.getTypeElement(Constants.COMPONENT) != null;
}
diAnnotation = (useComponent ? "@Component" : "@Singleton");

useJsonb = elementUtils.getTypeElement("io.avaje.jsonb.Jsonb") != null;
final var javax = elementUtils.getTypeElement(Constants.SINGLETON_JAVAX) != null;
final var jakarta = elementUtils.getTypeElement(Constants.SINGLETON_JAKARTA) != null;
final var override = options.get("useJavax");
Expand Down Expand Up @@ -186,6 +187,10 @@ public static boolean instrumentAllWebMethods() {
return CTX.get().instrumentAllMethods;
}

public static boolean useJsonb() {
return CTX.get().useJsonb;
}

public static boolean disabledDirectWrites() {
return CTX.get().disableDirectWrites;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@

public class JavalinProcessor extends BaseProcessor {

private final boolean useJsonB;

public JavalinProcessor() {
useJsonB = JsonBUtil.detectJsonb();
}

public JavalinProcessor(boolean useJsonb) {
useJsonB = useJsonb;
}

@Override
protected PlatformAdapter providePlatformAdapter() {
return new JavalinAdapter(useJsonB);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@

public class NimaProcessor extends BaseProcessor {

private final boolean jsonB;

public NimaProcessor() {
jsonB = JsonBUtil.detectJsonb();
}

public NimaProcessor(boolean useJsonb) {
jsonB = useJsonb;
}

@Override
protected PlatformAdapter providePlatformAdapter() {
return new NimaPlatformAdapter();
}

@Override
public void writeControllerAdapter(ControllerReader reader) throws IOException {
new ControllerWriter(reader, jsonB).write();
new ControllerWriter(reader, useJsonB).write();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public void runAnnotationProcessor() throws Exception {

final var task =
compiler.getTask(
new PrintWriter(System.out), null, null, List.of("--release=11"), null, files);
new PrintWriter(System.out),
null,
null,
List.of("--release=11", "-AdisableDirectWrites=true"),
null,
files);
task.setProcessors(List.of(new JavalinProcessor()));

assertThat(task.call()).isTrue();
Expand All @@ -71,7 +76,7 @@ public void runAnnotationProcessorJsonB() throws Exception {
final var task =
compiler.getTask(
new PrintWriter(System.out), null, null, List.of("--release=11"), null, files);
task.setProcessors(List.of(new JavalinProcessor(true), new Processor()));
task.setProcessors(List.of(new JavalinProcessor(), new Processor()));

assertThat(task.call()).isTrue();
assert Files.readString(
Expand All @@ -92,10 +97,14 @@ public void runAnnotationProcessorJavax() throws Exception {
new PrintWriter(System.out),
null,
null,
List.of("--release=11", "-AuseJavax=true", "-AuseSingleton=true"),
List.of(
"--release=11",
"-AuseJavax=true",
"-AuseSingleton=true",
"-AdisableDirectWrites=true"),
null,
files);
task.setProcessors(List.of(new JavalinProcessor(false), new Processor()));
task.setProcessors(List.of(new JavalinProcessor(), new Processor()));
// we don't have javax on the cp
assertThat(task.call()).isFalse();

Expand All @@ -117,10 +126,14 @@ public void runAnnotationProcessorJakarta() throws Exception {
new PrintWriter(System.out),
null,
null,
List.of("--release=11", "-AuseJavax=false", "-AuseSingleton=true"),
List.of(
"--release=11",
"-AuseJavax=false",
"-AuseSingleton=true",
"-AdisableDirectWrites=true"),
null,
files);
task.setProcessors(List.of(new JavalinProcessor(false), new Processor()));
task.setProcessors(List.of(new JavalinProcessor(), new Processor()));

assertThat(task.call()).isTrue();

Expand All @@ -147,10 +160,10 @@ public void testOpenAPIGeneration() throws Exception {
new PrintWriter(System.out),
null,
null,
List.of("--release=11"),
List.of("--release=11", "-AdisableDirectWrites=true"),
null,
openAPIController);
task.setProcessors(List.of(new JavalinProcessor(false), new Processor()));
task.setProcessors(List.of(new JavalinProcessor(), new Processor()));

assertThat(task.call()).isTrue();

Expand Down Expand Up @@ -181,10 +194,10 @@ public void testInheritableOpenAPIGeneration() throws Exception {
new PrintWriter(System.out),
null,
null,
List.of("--release=11"),
List.of("--release=11", "-AdisableDirectWrites=true"),
null,
openAPIController);
task.setProcessors(List.of(new JavalinProcessor(false), new Processor()));
task.setProcessors(List.of(new JavalinProcessor(), new Processor()));

assertThat(task.call()).isTrue();

Expand All @@ -196,7 +209,6 @@ public void testInheritableOpenAPIGeneration() throws Exception {
assert expectedOpenApiJson.equals(generatedOpenApi);
}


private Iterable<JavaFileObject> getSourceFiles(String source) throws Exception {
final var compiler = ToolProvider.getSystemJavaCompiler();
final var files = compiler.getStandardFileManager(null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ void runAnnotationProcessor() throws Exception {

final var task =
compiler.getTask(
new PrintWriter(System.out), null, null, List.of("--release=19"), null, files);
task.setProcessors(List.of(new NimaProcessor(false)));
new PrintWriter(System.out), null, null, List.of("--release=20", "-AdisableDirectWrites=true"), null, files);
task.setProcessors(List.of(new NimaProcessor()));

assertThat(task.call()).isTrue();
}
Expand Down