Skip to content

Commit

Permalink
Add MessageBodyHandlerRegistry#getReader which will fail on missing…
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Aug 30, 2024
1 parent d10e6f7 commit 7e222ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
import io.micronaut.http.client.netty.ssl.NettyClientSslBuilder;
import io.micronaut.http.client.netty.websocket.NettyWebSocketClientHandler;
import io.micronaut.http.client.sse.SseClient;
import io.micronaut.http.codec.CodecException;
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.http.context.ContextPathUtils;
import io.micronaut.http.context.ServerRequestContext;
Expand Down Expand Up @@ -774,7 +773,7 @@ public <I, B> Publisher<Event<B>> eventStream(@NonNull io.micronaut.http.HttpReq
@Override
public <I, B> Publisher<Event<B>> eventStream(@NonNull io.micronaut.http.HttpRequest<I> request, @NonNull Argument<B> eventType, @NonNull Argument<?> errorType) {
setupConversionService(request);
MessageBodyReader<B> reader = handlerRegistry.findReader(eventType, List.of(MediaType.APPLICATION_JSON_TYPE)).orElseThrow(() -> new CodecException("JSON codec not present"));
MessageBodyReader<B> reader = handlerRegistry.getReader(eventType, List.of(MediaType.APPLICATION_JSON_TYPE));
return Flux.from(eventStreamOrError(request, errorType)).map(byteBufferEvent -> {
ByteBuffer<?> data = byteBufferEvent.getData();

Expand Down Expand Up @@ -999,7 +998,7 @@ private <I, O> Flux<O> jsonStreamImpl(io.micronaut.http.HttpRequest<?> parentReq

// could also be application/json, in which case we will stream an array
MediaType mediaType = response.getContentType().orElse(MediaType.APPLICATION_JSON_STREAM_TYPE);
ChunkedMessageBodyReader<O> reader = (ChunkedMessageBodyReader<O>) handlerRegistry.findReader(type, List.of(mediaType)).orElseThrow(() -> new CodecException("Codec missing for media type " + mediaType));
ChunkedMessageBodyReader<O> reader = (ChunkedMessageBodyReader<O>) handlerRegistry.getReader(type, List.of(mediaType));
return reader.readChunked(type, mediaType, response.getHeaders(), Flux.from(streamResponse).map(c -> NettyByteBufferFactory.DEFAULT.wrap(c.content())));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public <T> Optional<MessageBodyWriter<T>> findWriter(Argument<T> type, List<Medi

};

/**
* Find a reader for the type and annotation metadata at declaration point.
* @param type The type
* @param mediaType The media type
* @return A message body reader if it is existing.
* @param <T> The generic type
*/
@NonNull
default <T> MessageBodyReader<T> getReader(@NonNull Argument<T> type, @Nullable List<MediaType> mediaType) {
return findReader(type, mediaType).orElseThrow(() -> new CodecException("Cannot read value of argument [" + type + "]. No possible readers found for media type: " + mediaType));
}

/**
* Find a reader for the type and annotation metadata at declaration point.
* @param type The type
Expand Down Expand Up @@ -125,6 +137,7 @@ default <T> Optional<MessageBodyWriter<T>> findWriter(@NonNull Argument<T> type)
* @return A message body writer if it is existing.
* @param <T> The generic type
*/
@NonNull
default <T> MessageBodyWriter<T> getWriter(@NonNull Argument<T> type,
@NonNull List<MediaType> mediaType) {
return findWriter(type, mediaType)
Expand Down

0 comments on commit 7e222ca

Please sign in to comment.