From f9bf26247ae664e881f62033f6d99906f232e689 Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Wed, 27 Nov 2024 14:54:08 +0100 Subject: [PATCH] XWIKI-22702: Provide parentType information in EntityReference * Provide EntityReference#getParentType * Use it in AbstractReferenceEntityReferenceResolver * Provide a new test --- ...tractReferenceEntityReferenceResolver.java | 14 +++-- .../model/reference/EntityReference.java | 60 +++++++++++++++++-- ...tReferenceEntityReferenceResolverTest.java | 22 +++++-- .../model/reference/EntityReferenceTest.java | 36 +++++++++++ 4 files changed, 120 insertions(+), 12 deletions(-) diff --git a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractReferenceEntityReferenceResolver.java b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractReferenceEntityReferenceResolver.java index c62aa22c9052..0ed295203a8b 100644 --- a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractReferenceEntityReferenceResolver.java +++ b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/internal/reference/AbstractReferenceEntityReferenceResolver.java @@ -90,18 +90,19 @@ private EntityReference normalizeReference(EntityReference referenceToResolve, O EntityReference reference = normalizedReference; while (reference != null) { List types = reference.getType().getAllowedParents(); - if (reference.getParent() != null && !types.isEmpty() && !types.contains(reference.getParent().getType())) { + if (reference.getParent() != null + && isParentTypeAndAllowedTypeNotMatching(types, reference.getParentType())) { // The parent reference isn't the allowed parent: insert an allowed reference EntityReference newReference = resolveDefaultReference(types.get(0), parameters).appendParent(reference.getParent()); normalizedReference = normalizedReference.replaceParent(reference.getParent(), newReference); reference = newReference; - } else if (reference.getParent() == null && !types.isEmpty()) { + } else if (reference.getParent() == null && reference.getParentType() != null) { // The top reference isn't the allowed top level reference, add a parent reference - EntityReference newReference = resolveDefaultReference(types.get(0), parameters); + EntityReference newReference = resolveDefaultReference(reference.getParentType(), parameters); normalizedReference = normalizedReference.appendParent(newReference); reference = newReference; - } else if (reference.getParent() != null && types.isEmpty()) { + } else if (reference.getParent() != null && (types.isEmpty() || reference.getParentType() == null)) { // There's a parent but no one is allowed throw new InvalidEntityReferenceException(); } else { @@ -112,4 +113,9 @@ private EntityReference normalizeReference(EntityReference referenceToResolve, O return normalizedReference; } + + private boolean isParentTypeAndAllowedTypeNotMatching(List allowedTypes, EntityType parentType) + { + return !allowedTypes.isEmpty() && parentType != null && !allowedTypes.contains(parentType); + } } diff --git a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/reference/EntityReference.java b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/reference/EntityReference.java index f5abe926e63e..935d84617384 100644 --- a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/reference/EntityReference.java +++ b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/main/java/org/xwiki/model/reference/EntityReference.java @@ -33,6 +33,7 @@ import org.xwiki.model.EntityType; import org.xwiki.model.internal.reference.DefaultSymbolScheme; import org.xwiki.model.internal.reference.LocalizedStringEntityReferenceSerializer; +import org.xwiki.stability.Unstable; /** * Represents a reference to an Entity (Document, Attachment, Space, Wiki, etc). @@ -42,6 +43,13 @@ */ public class EntityReference implements Serializable, Cloneable, Comparable { + /** + * See {@link #getParentType()}. + * @since 17.0.0RC1 + */ + @Unstable + public static final String PARENT_TYPE_PARAMETER = "parentType"; + /** * Used to provide a nice and readable pretty name for the {@link #toString()} method. */ @@ -117,7 +125,6 @@ protected EntityReference(EntityReference reference, EntityReference oldReferenc setName(reference.name); setType(reference.type); - setParameters(reference.parameters); if (reference.parent == null) { if (oldReference == null) { setParent(newReference); @@ -130,6 +137,7 @@ protected EntityReference(EntityReference reference, EntityReference oldReferenc } else { setParent(new EntityReference(reference.parent, oldReference, newReference)); } + setParameters(reference.parameters); } /** @@ -190,7 +198,7 @@ public EntityReference(String name, EntityType type, EntityReference parent, Map } /** - * Clone an EntityReference, but use the specified paramaters. + * Clone an EntityReference, but use the specified parameters. * * @param reference the reference to clone * @param parameters parameters for this reference, may be null @@ -297,15 +305,34 @@ protected void setParameter(String name, Serializable value) if (this.parameters == null) { this.parameters = new TreeMap<>(); } - this.parameters.put(name, value); + if (PARENT_TYPE_PARAMETER.equals(name)) { + setParentTypeParameter(value); + } else { + this.parameters.put(name, value); + } } else if (parameters != null) { this.parameters.remove(name); - if (this.parameters.size() == 0) { + if (this.parameters.isEmpty()) { this.parameters = null; } } } + private void setParentTypeParameter(Serializable value) + { + if (value != null && getParent() == null) { + EntityType parentType = null; + if (value instanceof EntityType entityType) { + parentType = entityType; + } else if (value instanceof String entityTypeString) { + parentType = EntityType.valueOf(entityTypeString); + } + if (parentType != null && getType().getAllowedParents().contains(parentType)) { + this.parameters.put(PARENT_TYPE_PARAMETER, parentType); + } + } + } + /** * Get the value of a parameter. Return null if the parameter is not set. This method is final so there is no way to * override the map, and the private field in all other methods of this implementation (faster). @@ -679,6 +706,31 @@ public boolean equalsNonRecursive(EntityReference otherReference) && (parameters == null ? otherReference.parameters == null : parameters.equals(otherReference.parameters)); } + /** + * The parent type information is used by resolvers to identify which part of the base reference should be kept. + * If the entity reference has a parent (see {@link #getParent()}) then this type should always be the type of + * the parent. Now if the entity reference doesn't have the parent this value can be given by the + * {@link #PARENT_TYPE_PARAMETER} parameter (see {@link #getParameter(String)}), and if none is given it will + * fall back on first allowed parents (see {@link EntityType#getAllowedParents()} of current type returned by + * {@link #getType()}. + * @return the type of the parent to be used for computing the proper base reference in resolvers. + * @since 17.0.0RC1 + */ + @Unstable + public EntityType getParentType() + { + EntityType result; + if (getParent() == null) { + result = getParameter(PARENT_TYPE_PARAMETER); + if (result == null && !getType().getAllowedParents().isEmpty()) { + result = getType().getAllowedParents().get(0); + } + } else { + result = getParent().getType(); + } + return result; + } + @Override public int hashCode() { diff --git a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/DefaultReferenceEntityReferenceResolverTest.java b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/DefaultReferenceEntityReferenceResolverTest.java index 9717c5dbdd91..9181fdf985be 100644 --- a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/DefaultReferenceEntityReferenceResolverTest.java +++ b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/internal/reference/DefaultReferenceEntityReferenceResolverTest.java @@ -19,7 +19,8 @@ */ package org.xwiki.model.internal.reference; -import java.util.Arrays; +import java.util.List; +import java.util.Map; import org.junit.jupiter.api.Test; import org.xwiki.model.EntityType; @@ -303,7 +304,7 @@ public void resolveDocumentReferenceWhenTypeIsPage() new EntityReference("page1", EntityType.PAGE, new EntityReference("page2", EntityType.PAGE)), EntityType.DOCUMENT); - assertEquals(new DocumentReference(DEFAULT_WIKI, Arrays.asList("page2", "page1"), DEFAULT_DOCUMENT), reference); + assertEquals(new DocumentReference(DEFAULT_WIKI, List.of("page2", "page1"), DEFAULT_DOCUMENT), reference); } @Test @@ -312,12 +313,25 @@ public void resolveSpaceReferenceWhenTypeIsPage() EntityReference reference = this.resolver.resolve(new EntityReference("page", EntityType.PAGE), EntityType.SPACE); - assertEquals(new SpaceReference(DEFAULT_WIKI, Arrays.asList("page")), reference); + assertEquals(new SpaceReference(DEFAULT_WIKI, List.of("page")), reference); reference = this.resolver.resolve( new EntityReference("page1", EntityType.PAGE, new EntityReference("page2", EntityType.PAGE)), EntityType.SPACE); - assertEquals(new SpaceReference(DEFAULT_WIKI, Arrays.asList("page2", "page1")), reference); + assertEquals(new SpaceReference(DEFAULT_WIKI, List.of("page2", "page1")), reference); + } + + @Test + void resolveRelativeEntityReference() + { + // When the space reference has a parent type parameter of tye space, then it's resolved using the base + // reference space has parent of it. + EntityReference relativeSpaceReference = new EntityReference("Space", EntityType.SPACE, + Map.of(EntityReference.PARENT_TYPE_PARAMETER, EntityType.SPACE)); + + EntityReference reference = this.resolver.resolve(relativeSpaceReference, EntityType.SPACE); + SpaceReference spaceReference = new SpaceReference(DEFAULT_WIKI, List.of(DEFAULT_SPACE, "Space")); + assertEquals(spaceReference, reference); } } diff --git a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/reference/EntityReferenceTest.java b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/reference/EntityReferenceTest.java index 177924e8180e..4be3fa41a671 100644 --- a/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/reference/EntityReferenceTest.java +++ b/xwiki-platform-core/xwiki-platform-model/xwiki-platform-model-api/src/test/java/org/xwiki/model/reference/EntityReferenceTest.java @@ -630,4 +630,40 @@ void removeParametersRecursive() assertSame(reference, reference.removeParameters(false)); assertEquals(parentWithoutParameters, parent.removeParameters(true)); } + + @Test + void getParentType() + { + // no parent, no custom type, and no fallback for wiki + EntityReference wikiReference = new EntityReference(WIKI_NAME, EntityType.WIKI); + assertNull(wikiReference.getParentType()); + + // parent type + EntityReference spaceReference = new EntityReference(SPACE_NAME, EntityType.SPACE, wikiReference); + assertEquals(EntityType.WIKI, spaceReference.getParentType()); + + // parent is set, so custom type cannot be set + spaceReference = + new EntityReference(spaceReference, Map.of(EntityReference.PARENT_TYPE_PARAMETER, EntityType.SPACE)); + assertEquals(EntityType.WIKI, spaceReference.getParentType()); + + // no parent, no custom type, but fallback on first allowed parent type + spaceReference = new EntityReference(SPACE_NAME, EntityType.SPACE); + assertEquals(EntityType.WIKI, spaceReference.getParentType()); + + // custom type + spaceReference = + new EntityReference(spaceReference, Map.of(EntityReference.PARENT_TYPE_PARAMETER, EntityType.SPACE)); + assertEquals(EntityType.SPACE, spaceReference.getParentType()); + + // custom type + spaceReference = new EntityReference(SPACE_NAME, EntityType.SPACE, + Map.of(EntityReference.PARENT_TYPE_PARAMETER, "SPACE")); + assertEquals(EntityType.SPACE, spaceReference.getParentType()); + + // invalid parent type, fallback on first allowed parent type + spaceReference = new EntityReference(SPACE_NAME, EntityType.SPACE, + Map.of(EntityReference.PARENT_TYPE_PARAMETER, EntityType.PAGE)); + assertEquals(EntityType.WIKI, spaceReference.getParentType()); + } }