Relations fail with "field 'task.project' cannot be accessed for POST"
See original GitHub issueHi, didn’t have to say it yet, but thanks for this great library 👍
I’m trying to map relationships to a classes already mapped to Objectify using the standard @JsonApiRelation
and @JsonApiRelationId
annotations, but for an unknown reason, I get this kind of message as soon as I try to run some commands (be it POST, PATCH or DELETE) :
{
"errors": [
{
"status": "403",
"title": "FORBIDDEN",
"detail": "field 'task.project' cannot be accessed for POST"
}
]
}
I’m well aware that they’d rather be splitted into model and viewmodel classes, but that’s what I have for now (and that’s beyond the point I guess).
Here is a code sample (greatly inspired from the examples in the doc) :
@JsonApiResource(type = "task")
@Entity
public class Task {
@JsonApiId
@Id
private String id;
private Ref<Project> project;
@JsonApiRelationId
private String projectId;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@JsonApiRelation
public Project getProject() {
return project != null ? project.get() : null;
}
@JsonApiRelation
public void setProject(Project project) {
this.project = project != null ? Ref.create(project) : null;
this.projectId = projectId != null ? project.getId() : null;
}
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
this.project = null;
}
}
@JsonApiResource(type = "project")
@Entity
public class Project {
@JsonApiId
@Id
private String id;
private Set<Ref<Task>> tasks;
@JsonApiRelationId
private Set<String> taskIds;
public Project() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@JsonApiRelation(mappedBy = "project")
public Set<Task> getTasks() {
return tasks == null ? null :
tasks.stream()
.map(Ref::get)
.collect(toSet());
}
@JsonApiRelation(mappedBy = "project")
public void setTasks(Set<Task> tasks) {
this.tasks = tasks == null ? null :
tasks.stream()
.map(Ref::create)
.collect(toSet());
}
public Set<String> getTaskIds() {
return taskIds;
}
public void setTaskIds(Set<String> tasksId) {
this.taskIds = tasksId;
this.tasks = null;
}
}
There is a resource repo for those classes as well (it’s longer, but let me know if you need it), extending ResourceRepositoryBase
and overriding all the CRUD methods (which works well), but no relationship repo, as I read in the doc that for this kind of standard use case they were not necessary.
Am I missing something obvious here ? Should I write relationships repos anyway ?
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
I guess it would be good to have a closer look at the BeanInformation class. It can access field for historic reasons, maybe it should not do that anymore. just rely on getter/setter
I’ll let you decide whether this issue should be closed or not then 😇