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

Make RepositoryEntityMultipartHttpMessageConverter resolve json HttpMessageConverter when it is being used. #2189

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.springframework.data.rest.extensions.entitycontent;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.InvalidPropertyException;
import org.springframework.data.rest.webmvc.PersistentEntityResource;
import org.springframework.hateoas.RepresentationModel;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
Expand All @@ -27,24 +24,28 @@

public class RepositoryEntityMultipartHttpMessageConverter implements HttpMessageConverter<Object> {

private HttpMessageConverter converter = null;
private final List<HttpMessageConverter<?>> converters;

public RepositoryEntityMultipartHttpMessageConverter(List<HttpMessageConverter<?>> messageConverters) {
for (HttpMessageConverter converter : messageConverters) {
if (converter.canRead(RepresentationModel.class, MediaType.APPLICATION_JSON)) {
this.converter = converter;
break;
this.converters = messageConverters;
}

private HttpMessageConverter<?> findReadConverter(Class clazz) {
for (HttpMessageConverter<?> converter : converters) {
if(converter.canRead(clazz, MediaType.APPLICATION_JSON)) {
return converter;
}
}
if (this.converter == null) {
throw new IllegalStateException("unable to resolve persistent entity resource message converter");
}
return null;
}

@Override
public boolean canRead(Class clazz, MediaType mediaType) {
return PersistentEntityResource.class.equals(clazz) && mediaType != null
&& mediaType.includes(MediaType.MULTIPART_FORM_DATA);
if(mediaType == null || !mediaType.includes(MediaType.MULTIPART_FORM_DATA)) {
return false;
}

return findReadConverter(clazz) != null;
}

@Override
Expand All @@ -57,11 +58,6 @@ public List<MediaType> getSupportedMediaTypes() {
return Collections.singletonList(MediaType.MULTIPART_FORM_DATA);
}

@Override
public List<MediaType> getSupportedMediaTypes(Class clazz) {
return HttpMessageConverter.super.getSupportedMediaTypes(clazz);
}

@Override
public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
if (inputMessage instanceof ServletServerHttpRequest == false) {
Expand Down Expand Up @@ -93,7 +89,12 @@ public Object read(Class clazz, HttpInputMessage inputMessage) throws IOExceptio
HttpHeaders headers = new HttpHeaders(inputMessage.getHeaders());
headers.setContentType(MediaType.APPLICATION_JSON);

return this.converter.read(clazz, new HttpInputMessage() {
var converter = findReadConverter(clazz);
if(converter == null) {
throw new HttpMessageNotReadableException("No converter found for reading", inputMessage);
}

return converter.read(clazz, new HttpInputMessage() {
@Override
public InputStream getBody() throws IOException {
ObjectMapper mapper = new ObjectMapper();
Expand Down
Loading