spring-cloud/spring-cloud-function

Issue in routingFunction when passing Parent reference containing child object while routing to function

Mohit-Sardiwal opened this issue · 2 comments

Describe the bug
Hi Team,

I am using RoutingFunction of the spring cloud.
(this is link to the sample code base -https://github.com/Mohit-Sardiwal/inheritance
which I am explaining below)

Lets say I have Parent class , Child class which extends Parent and GrandChild which extends Child class.
I am using parent reference and GrandChild object to store the below json string payload

 ObjectMapper objectMapper = new ObjectMapper();
Object jsonObject = objectMapper.readValue(jsonString, GrandChild.class);
Parent parent = (Parent)jsonObject ;
{
	"id": "4a001",
	"name": "John",
	"childId": "12345",
	"value": "first",
	"properties": {
		"hobbies": [
			{
				"hobbyId": "http",
				"hobbyName": "http"
			}
		]
	}
}

I am making list of the above Parent object and making spring message out of it and passing that message to routingFunction

List parentList = new ArrayList<>();
parentList.add(parent);

Message object = (Message) MessageBuilder.withPayload(parentList).build();

Message<T> functionMessage = MessageBuilder
.withPayload(object.getPayload())
.setHeader("spring.cloud.function.definition", "getFamilyData")
.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain")
.copyHeadersIfAbsent(object.getHeaders())
.build();**

in my function (getFamilyData) I am expecting list of Child (as shown below). But I when I am using CONTENT_TYPE as text/plain I am getting empty list in FxGetFamilyData function and when I am not passing CONTENT_TYPE then I am getting list but in that I am only getting Child data . Grand Child data is not there.

**@Bean
  public Function<List<Child>, List<Child>> getFamilyData() {
    return new FxGetFamilyData();
  }**

**public class FxGetFamilyData implements UnaryOperator<List<Child>> {
  @Override
  public List<Child> apply(List<Child> children) {
    log.debug("Received Event :{}",children);
    return children;
  }
}

When ContentType is text/plain:
Received Event :[]

When ContentType is not send in headers (Not able to receive GrandChild data)
Received Event :[Child(childId=12345, value=first)]**

Classes

public class Parent {
  private String id;
  private String name;
}

public class Child extends Parent{
  private String childId;
  private String value;
}

public class GrandChild extends Child{
  private GrandChildProperties properties;
}

public class GrandChildProperties {
  private List<Hobbies> hobbies;
}

public class Hobbies {
  private String hobbyId;
  private String hobbyName;
}

Not passing any content type will default to application/json. Passing text/plain will not work here and should not work. I'd be surprised if it did. You are passing json and expect what in terms of text/plain?
In any case with application/json i get

[childId: 12345 value: first id: 4a001 name: John]

So I am not sure what doesn't work in your sample, but also it has nothing to do with RoutingFunction since it properly delegates to an appropriate function based on spring.cloud.function.definition header.

Closing it do to inactivity from the original reporter