Skip to content

Commit

Permalink
flag to disable jsonb direct writes
Browse files Browse the repository at this point in the history
  • Loading branch information
SentryMan committed Jun 25, 2023
1 parent e7b8560 commit 46ae11e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

@SupportedOptions({"useJavax", "useSingleton", "instrumentRequests"})
@SupportedOptions({"useJavax", "useSingleton", "instrumentRequests","disableDirectWrites"})
public abstract class BaseProcessor extends AbstractProcessor {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private static final class Ctx {
private final boolean useJavax;
private final String diAnnotation;
private final boolean instrumentAllMethods;
private final boolean disableDirectWrites;

Ctx(ProcessingEnvironment env, PlatformAdapter adapter, boolean generateOpenAPI) {
readAdapter = adapter;
Expand All @@ -57,6 +58,7 @@ private static final class Ctx {
final var options = env.getOptions();
final var singletonOverride = options.get("useSingleton");
this.instrumentAllMethods = Boolean.parseBoolean(options.get("instrumentRequests"));
this.disableDirectWrites = Boolean.parseBoolean(options.get("disableDirectWrites"));
if (singletonOverride != null) {
useComponent = !Boolean.parseBoolean(singletonOverride);
} else {
Expand Down Expand Up @@ -184,6 +186,10 @@ public static boolean instrumentAllWebMethods() {
return CTX.get().instrumentAllMethods;
}

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

public static Filer filer() {
return CTX.get().filer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.avaje.http.generator.javalin;

import static io.avaje.http.generator.core.ProcessingContext.platform;
import static io.avaje.http.generator.core.ProcessingContext.*;

import io.avaje.http.generator.core.Append;
import io.avaje.http.generator.core.MethodParam;
Expand All @@ -26,7 +26,7 @@ class ControllerMethodWriter {
this.method = method;
this.writer = writer;
this.webMethod = method.webMethod();
this.useJsonB = useJsonB;
this.useJsonB = useJsonB && !disabledDirectWrites();
this.instrumentContext = method.instrumentContext();
}

Expand Down Expand Up @@ -134,10 +134,16 @@ private void writeContextReturn(final String resultVariableName) {
if (produces == null || MediaType.APPLICATION_JSON.getValue().equalsIgnoreCase(produces)) {
if (useJsonB) {
var uType = UType.parse(method.returnType());
if ("java.util.concurrent.CompletableFuture".equals(uType.mainType())) {
final boolean isfuture = "java.util.concurrent.CompletableFuture".equals(uType.mainType());
if (isfuture) {
uType = uType.paramRaw();
writer.append(" try {");
}
writer.append(" %sJsonType.toJson(%s, ctx.contentType(\"application/json\").res().getOutputStream());",
uType.shortName(), resultVariableName);
if (isfuture) {
writer.append(" } catch (java.io.IOException e) { throw new java.io.UncheckedIOException(e); }");
}
writer.append(" %sJsonType.toJson(%s, ctx.contentType(\"application/json\").res().getOutputStream());", uType.shortName(), resultVariableName);
} else {
writer.append(" ctx.json(%s);", resultVariableName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.avaje.http.generator.helidon.nima;

import static io.avaje.http.generator.core.ProcessingContext.disabledDirectWrites;
import static io.avaje.http.generator.core.ProcessingContext.platform;

import java.util.List;
Expand Down Expand Up @@ -140,8 +141,9 @@ void writeHandler(boolean requestScoped) {

private boolean producesJson() {
return useJsonB
&& !"byte[]".equals(method.returnType().toString())
&& (method.produces() == null || method.produces().toLowerCase().contains("json"));
&& !disabledDirectWrites()
&& !"byte[]".equals(method.returnType().toString())
&& (method.produces() == null || method.produces().toLowerCase().contains("json"));
}

private boolean missingServerResponse(List<MethodParam> params) {
Expand Down

0 comments on commit 46ae11e

Please sign in to comment.