From 5d3546b553d83554862fb72004693d2cfc3938ec Mon Sep 17 00:00:00 2001 From: Kyle Bishop Date: Wed, 11 Dec 2019 09:07:13 -0700 Subject: [PATCH] InitNesting on subtypes, reuse verifyTypes for buildNewResource, change FieldResourcePost to use body as type instead of path as type to support subtypes, allow setupManyNesting to set oppositeName on parentField if shouldBeNested and the oppositeName is null --- .../src/main/java/io/crnk/core/boot/CrnkBoot.java | 1 + .../information/resource/ResourceInformation.java | 3 +++ .../dispatcher/controller/FieldResourcePost.java | 11 +++++------ .../dispatcher/controller/ResourceUpsert.java | 8 +++----- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/crnk-core/src/main/java/io/crnk/core/boot/CrnkBoot.java b/crnk-core/src/main/java/io/crnk/core/boot/CrnkBoot.java index e5d85ff52..dd5b012ed 100644 --- a/crnk-core/src/main/java/io/crnk/core/boot/CrnkBoot.java +++ b/crnk-core/src/main/java/io/crnk/core/boot/CrnkBoot.java @@ -240,6 +240,7 @@ private List toSimpleNames(List implementations) { private void setupRepositories(ResourceRegistryPart rootPart) { for (RegistryEntry entry : moduleRegistry.getRegistryEntries()) { rootPart.addEntry(entry); + entry.getResourceInformation().initNesting(); } } diff --git a/crnk-core/src/main/java/io/crnk/core/engine/information/resource/ResourceInformation.java b/crnk-core/src/main/java/io/crnk/core/engine/information/resource/ResourceInformation.java index 4c90f6903..a94760434 100644 --- a/crnk-core/src/main/java/io/crnk/core/engine/information/resource/ResourceInformation.java +++ b/crnk-core/src/main/java/io/crnk/core/engine/information/resource/ResourceInformation.java @@ -291,6 +291,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; diff --git a/crnk-core/src/main/java/io/crnk/core/engine/internal/dispatcher/controller/FieldResourcePost.java b/crnk-core/src/main/java/io/crnk/core/engine/internal/dispatcher/controller/FieldResourcePost.java index b1912c4fb..2ebe8dda4 100644 --- a/crnk-core/src/main/java/io/crnk/core/engine/internal/dispatcher/controller/FieldResourcePost.java +++ b/crnk-core/src/main/java/io/crnk/core/engine/internal/dispatcher/controller/FieldResourcePost.java @@ -56,7 +56,6 @@ public Result handleAsync(JsonPath jsonPath, QueryAdapter queryAdapter RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType()); ResourceInformation bodyResourceInformation = bodyRegistryEntry.getResourceInformation(); - Serializable id = jsonPath.getId(); ResourceField relationshipField = fieldPath.getField(); @@ -69,13 +68,13 @@ public Result 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 createdResource = setRelationsAsync(entity, bodyRegistryEntry, resourceBody, queryAdapter, false) .merge(it -> resourceRepository.create(entity, queryAdapter)); diff --git a/crnk-core/src/main/java/io/crnk/core/engine/internal/dispatcher/controller/ResourceUpsert.java b/crnk-core/src/main/java/io/crnk/core/engine/internal/dispatcher/controller/ResourceUpsert.java index 41865e53e..2a8606317 100644 --- a/crnk-core/src/main/java/io/crnk/core/engine/internal/dispatcher/controller/ResourceUpsert.java +++ b/crnk-core/src/main/java/io/crnk/core/engine/internal/dispatcher/controller/ResourceUpsert.java @@ -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 instanceBuilder = registryEntry.getResourceInformation().getInstanceBuilder(); Object entity = instanceBuilder.buildResource(dataBody);