spring-projects / spring-projects/spring-data-rest

Can't POST a collection [DATAREST-315]

Open
#695 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

in: repository type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Zachariah Young opened DATAREST-315 and commented

I have a simple Entity with a single collection mapped.

@Entity
public class Appointment Identifiable<Integer> {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonIgnore
private Integer id;

@Column(name="TRAK_NBR")
private String trackNumber;

@OneToMany(fetch =FetchType.EAGER, cascade= CascadeType.ALL)
@JoinColumn(name="CNSM_APT_VER_WRK_I", nullable = false)
private Set<Product> products = new HashSet<Product>();

}

@Entity
public class Product implements Identifiable<Integer> {

@Id
@Column(name = "CNSM_PRD_VER_WRK_I")
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonIgnore
private Integer id;

@Column(name = "PRD_MDL_NBR")
private String model;

@Column(name = "PRD_SPEC_DSC")
private String description;

}

In my application when I only include a PagingAndSortingRepository for Appointment. I can call the POST command with the following payload.

{
"trackNumber" : "XYZ123",
"products": [
{"model" : "MODEL",
"description" : "NAME"
}]
}

When I add a PagingAndSortingRepository for Product and try the same POST I get the following error message.

{
"cause" : {
"cause" : {
"cause" : null,
"message" : null
},
"message" : "(was java.lang.NullPointerException) (through reference chain: com..model.Appointment["products"])"
},
"message" : "Could not read JSON: (was java.lang.NullPointerException) (through reference chain: com.model.Appointment["products"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.model.AppointmentVerification["products"])"
}

My GET payload with both Repositories returns this. This is my desired format. The link to products should be included

{
"trackNumber" : "XYZ123",
"_links" : {
"self" : {
"href" : "http://localhost:8080/consumerappointment/appointments/70"
},
"products" : {
"href" : "http://localhost:8080/consumerappointment/appointments/70/products"
}
}

With only the Appointment repository I get the following payload and can post the list of products.

{
"trackNumber" : "XYZ123",
"products" : [ {
"model" : "MODEL",
"description" : "NAME",
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8080/consumerappointment/appointments/1"
}
}
}


Affects: 2.0.2 (Codd SR2)

Reference URL: http://forum.spring.io/forum/data-rest/749962-how-to-post-payload-with-collection?_=1402065779813

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.