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

Fix posting to multiple nested related resource #658

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions crnk-core/src/main/java/io/crnk/core/boot/CrnkBoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ private List<String> toSimpleNames(List<?> implementations) {
private void setupRepositories(ResourceRegistryPart rootPart) {
for (RegistryEntry entry : moduleRegistry.getRegistryEntries()) {
rootPart.addEntry(entry);
entry.getResourceInformation().initNesting();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ private boolean setupManyNesting() {
parentAttribute.getName(), implementationClass);

((ResourceFieldImpl) parentField).setIdField(parentAttribute.getName(), parentAttribute.getImplementationClass(), parentIdAccessor);
if(null == ((ResourceFieldImpl)parentField).getOppositeName() && shouldBeNested()) {
((ResourceFieldImpl)parentField).setOppositeName(resourcePath);
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public Result<Response> handleAsync(JsonPath jsonPath, QueryAdapter queryAdapter
RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
ResourceInformation bodyResourceInformation = bodyRegistryEntry.getResourceInformation();


Serializable id = jsonPath.getId();
ResourceField relationshipField = fieldPath.getField();

Expand All @@ -69,13 +68,13 @@ public Result<Response> handleAsync(JsonPath jsonPath, QueryAdapter queryAdapter
DocumentMapper documentMapper = context.getDocumentMapper();

QueryContext queryContext = queryAdapter.getQueryContext();
Object entity = buildNewResource(relationshipRegistryEntry, resourceBody, relationshipResourceType);
Object entity = buildNewResource(bodyRegistryEntry, resourceBody, relationshipResourceType);

setId(resourceBody, entity, relationshipResourceInformation);
setId(resourceBody, entity, bodyResourceInformation);
setType(resourceBody, entity);
setAttributes(resourceBody, entity, relationshipResourceInformation, queryContext);
setMeta(resourceBody, entity, relationshipResourceInformation);
setLinks(resourceBody, entity, relationshipResourceInformation);
setAttributes(resourceBody, entity, bodyResourceInformation, queryContext);
setMeta(resourceBody, entity, bodyResourceInformation);
setLinks(resourceBody, entity, bodyResourceInformation);

Result<JsonApiResponse> createdResource = setRelationsAsync(entity, bodyRegistryEntry, resourceBody, queryAdapter, false)
.merge(it -> resourceRepository.create(entity, queryAdapter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,10 @@ protected boolean checkAccess() {


Object buildNewResource(RegistryEntry registryEntry, Resource dataBody, String resourceName) {
RegistryEntry relationshipRegistryEntry = context.getResourceRegistry().getEntry(resourceName);

PreconditionUtil.verify(dataBody != null, "No data field in the body.");
PreconditionUtil.verify(resourceName.equals(dataBody.getType()),
"Inconsistent type definition between path and body: body type: " +
"%s, request type: %s",
dataBody.getType(),
resourceName);
verifyTypes(HttpMethod.POST, relationshipRegistryEntry, registryEntry);

ResourceInstanceBuilder<Object> instanceBuilder = registryEntry.getResourceInformation().getInstanceBuilder();
Object entity = instanceBuilder.buildResource(dataBody);
Expand Down