Skip to content
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

Update to 4.1.1 & Rewrite MessageBodyWriter #1217

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ swagger-parser = "1.0.67"
swagger-parser-v3 = "2.1.16"
javaparser = "3.25.5"

micronaut = "4.1.2"
micronaut = "4.1.5"
micronaut-security = "4.1.0"
micronaut-serde = "2.2.4"
micronaut-rxjava2 = "2.0.1"
Expand All @@ -25,7 +25,7 @@ micronaut-validation = "4.0.3"
micronaut-data = "4.1.2"
micronaut-test = "4.0.2"
micronaut-kotlin = "4.0.2"
micronaut-platform = "4.0.5"
micronaut-platform = "4.1.1"
micronaut-logging = "1.1.2"

[libraries]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.micronaut.openapi.test.dated;


import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.annotation.Order;
import io.micronaut.core.type.Argument;
import io.micronaut.core.type.MutableHeaders;
Expand All @@ -11,7 +9,6 @@
import io.micronaut.http.body.MessageBodyHandlerRegistry;
import io.micronaut.http.body.MessageBodyWriter;
import io.micronaut.http.codec.CodecException;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import java.io.OutputStream;
Expand All @@ -30,32 +27,9 @@ final class DatedResponseBodyWriter<T> implements MessageBodyWriter<DatedRespons
private static final String LAST_MODIFIED_HEADER = "Last-Modified";

private final MessageBodyHandlerRegistry registry;
private final MessageBodyWriter<T> bodyWriter;
private final Argument<T> bodyType;

@Inject
DatedResponseBodyWriter(MessageBodyHandlerRegistry registry) {
this(registry, null, null);
}

private DatedResponseBodyWriter(
MessageBodyHandlerRegistry registry,
@Nullable MessageBodyWriter<T> bodyWriter,
@Nullable Argument<T> bodyType
) {
this.registry = registry;
this.bodyWriter = bodyWriter;
this.bodyType = bodyType;
}

@Override
public MessageBodyWriter<DatedResponse<T>> createSpecific(
Argument<DatedResponse<T>> type
) {
Argument<T> bt = type.getTypeParameters()[0];
MessageBodyWriter<T> writer = registry.findWriter(bt, List.of(MediaType.APPLICATION_JSON_TYPE))
.orElseThrow(() -> new ConfigurationException("No JSON message writer present"));
return new DatedResponseBodyWriter<>(registry, writer, bt);
}

@Override
Expand All @@ -66,27 +40,23 @@ public void writeTo(
MutableHeaders headers,
OutputStream outputStream
) throws CodecException {
if (bodyType != null && bodyWriter != null) {
if (dated.getLastModified() != null) {
headers.add(LAST_MODIFIED_HEADER, dated.getLastModified().toString());
}
bodyWriter.writeTo(bodyType, mediaType, dated.getBody(), headers, outputStream);
} else {
throw new ConfigurationException("No JSON message writer present");
Argument<T> bt = type.getTypeParameters()[0];
MessageBodyWriter<T> writer = registry.findWriter(bt, List.of(MediaType.APPLICATION_JSON_TYPE))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findWriter should be cached using createSpecific instead of doing it inside writeTo.

.orElseThrow(() -> new ConfigurationException("No JSON message writer present"));
if (dated.getLastModified() != null) {
headers.add(LAST_MODIFIED_HEADER, dated.getLastModified().toString());
}
writer.writeTo(bt, mediaType, dated.getBody(), headers, outputStream);
}

@Override
public boolean isWriteable(
Argument<DatedResponse<T>> type,
MediaType mediaType
) {
return bodyType != null && bodyWriter != null && bodyWriter.isWriteable(bodyType, mediaType);
}

@Override
public boolean isBlocking() {
return bodyWriter != null && bodyWriter.isBlocking();
Argument<T> bt = type.getTypeParameters()[0];
return registry.findWriter(bt, List.of(MediaType.APPLICATION_JSON_TYPE)).isPresent() &&
mediaType.equals(MediaType.APPLICATION_JSON_TYPE);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.micronaut.openapi.test.page;


import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.annotation.Order;
import io.micronaut.core.type.Argument;
import io.micronaut.core.type.MutableHeaders;
Expand All @@ -12,7 +10,6 @@
import io.micronaut.http.body.MessageBodyHandlerRegistry;
import io.micronaut.http.body.MessageBodyWriter;
import io.micronaut.http.codec.CodecException;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import java.io.OutputStream;
Expand All @@ -34,32 +31,9 @@ final class PageBodyWriter<T> implements MessageBodyWriter<Page<T>> {
private static final String PAGE_COUNT_HEADER = "X-Page-Count";

private final MessageBodyHandlerRegistry registry;
private final MessageBodyWriter<List<T>> bodyWriter;
private final Argument<List<T>> bodyType;

@Inject
PageBodyWriter(MessageBodyHandlerRegistry registry) {
this(registry, null, null);
}

private PageBodyWriter(
MessageBodyHandlerRegistry registry,
@Nullable MessageBodyWriter<List<T>> bodyWriter,
@Nullable Argument<List<T>> bodyType
) {
this.registry = registry;
this.bodyWriter = bodyWriter;
this.bodyType = bodyType;
}

@Override
public MessageBodyWriter<Page<T>> createSpecific(
Argument<Page<T>> type
) {
Argument<List<T>> bt = Argument.listOf(type.getTypeParameters()[0]);
MessageBodyWriter<List<T>> writer = registry.findWriter(bt, List.of(MediaType.APPLICATION_JSON_TYPE))
.orElseThrow(() -> new ConfigurationException("No JSON message writer present"));
return new PageBodyWriter<>(registry, writer, bt);
}

@Override
Expand All @@ -70,29 +44,24 @@ public void writeTo(
MutableHeaders headers,
OutputStream outputStream
) throws CodecException {
if (bodyType != null && bodyWriter != null) {
headers.add(PAGE_NUMBER_HEADER, String.valueOf(page.getPageNumber()));
headers.add(PAGE_SIZE_HEADER, String.valueOf(page.getSize()));
headers.add(PAGE_COUNT_HEADER, String.valueOf(page.getTotalPages()));
headers.add(TOTAL_COUNT_HEADER, String.valueOf(page.getTotalSize()));
Argument<List<T>> bt = Argument.listOf(type.getTypeParameters()[0]);
MessageBodyWriter<List<T>> writer = registry.findWriter(bt, List.of(MediaType.APPLICATION_JSON_TYPE))
.orElseThrow(() -> new ConfigurationException("No JSON message writer present"));

bodyWriter.writeTo(bodyType, mediaType, page.getContent(), headers, outputStream);
} else {
throw new ConfigurationException("No JSON message writer present");
}
headers.add(PAGE_NUMBER_HEADER, String.valueOf(page.getPageNumber()));
headers.add(PAGE_SIZE_HEADER, String.valueOf(page.getSize()));
headers.add(PAGE_COUNT_HEADER, String.valueOf(page.getTotalPages()));
headers.add(TOTAL_COUNT_HEADER, String.valueOf(page.getTotalSize()));
writer.writeTo(bt, mediaType, page.getContent(), headers, outputStream);
}

@Override
public boolean isWriteable(
Argument<Page<T>> type,
MediaType mediaType
) {
return bodyType != null && bodyWriter != null && bodyWriter.isWriteable(bodyType, mediaType);
}

@Override
public boolean isBlocking() {
return bodyWriter != null && bodyWriter.isBlocking();
Argument<List<T>> bt = Argument.listOf(type.getTypeParameters()[0]);
return mediaType.equals(MediaType.APPLICATION_JSON_TYPE) &&
registry.findWriter(bt, List.of(MediaType.APPLICATION_JSON_TYPE)).isPresent();
}

}
15 changes: 15 additions & 0 deletions test-suite-server-generator/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="error">
<appender-ref ref="STDOUT" />
</root>
<logger name="io.micronaut.configuration.openapi" level="TRACE"/>
</configuration>