Skip to content

Commit

Permalink
MID-8432: Fixed definition annotations in shallow clones
Browse files Browse the repository at this point in the history
Annotations were not properly stored in shallow Transformable*Definitions
and changes to annotations were made on underlying shared schema.

This fix intruduces shallow definitions also for annotations.
  • Loading branch information
tonydamage committed Jan 16, 2023
1 parent 62be02f commit 475a7c8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.evolveum.midpoint.schema.processor.deleg.ResourceObjectDefinitionDelegator;
import com.evolveum.midpoint.schema.processor.deleg.ResourceObjectTypeDefinitionDelegator;

public class TransformableComplexTypeDefinition implements ComplexTypeDefinitionDelegator, PartiallyMutableComplexTypeDefinition {
public class TransformableComplexTypeDefinition extends TransformableDefinition implements ComplexTypeDefinitionDelegator, PartiallyMutableComplexTypeDefinition {

private static final long serialVersionUID = 1L;
private static final TransformableItemDefinition<?, ?> REMOVED = new Removed();
Expand All @@ -33,6 +33,7 @@ public class TransformableComplexTypeDefinition implements ComplexTypeDefinition
private transient List<ItemDefinition<?>> definitionsCache;

public TransformableComplexTypeDefinition(ComplexTypeDefinition delegate) {
super(null);
var schemaDef = PrismContext.get().getSchemaRegistry().findComplexTypeDefinitionByType(delegate.getTypeName());
if (schemaDef == delegate) {
this.delegate = new DelegatedItem.StaticComplexType(delegate);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.model.impl.schema.transform;


import java.util.HashMap;
import java.util.Map;

import javax.xml.namespace.QName;

import org.jetbrains.annotations.Nullable;

import com.evolveum.midpoint.prism.Definition;
import com.evolveum.midpoint.prism.deleg.DefinitionDelegator;
import com.evolveum.midpoint.prism.util.CloneUtil;

public abstract class TransformableDefinition implements DefinitionDelegator {

private enum NullObject {
VALUE
}
private Map<QName, Object> annotations;

public TransformableDefinition(Definition delegate) {
if (delegate instanceof TransformableDefinition) {
// Shallow copy overriden annotations
Map<QName, Object> maybe = ((TransformableDefinition) delegate).annotationsOverrides();
if (maybe != null) {
annotations = new HashMap<>();
annotations.putAll(maybe);
}
}
}

@SuppressWarnings("unchecked")
@Override
public <A> A getAnnotation(QName qname) {
if (annotations != null) {
Object maybe = annotations.get(qname);
if (maybe != null) {
// If we NullObject.VALUE it is actually null, we store it to hide original annotation
// in parent item.
return maybe != NullObject.VALUE ? (A) maybe : null;
}
}
return DefinitionDelegator.super.getAnnotation(qname);
}

@Override
public <A> void setAnnotation(QName qname, A value) {
Object newVal = value != null ? value : NullObject.VALUE;
if (annotations == null) {
annotations = new HashMap<>();
}
annotations.put(qname, newVal);
}

protected Map<QName, Object> annotationsOverrides() {
return annotations;
}

@Override
public Definition clone() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.base.Preconditions;

public abstract class TransformableItemDefinition<I extends Item<?,?>,D extends ItemDefinition<I>>
extends TransformableDefinition
implements ItemDefinitionDelegator<I>, PrismItemAccessDefinition.Mutable, PartiallyMutableItemDefinition<I> {

private static final long serialVersionUID = 1L;
Expand All @@ -42,6 +43,7 @@ public abstract class TransformableItemDefinition<I extends Item<?,?>,D extends
private PrismReferenceValue valueEnumerationRef;

protected TransformableItemDefinition(D delegate) {
super(delegate);
if (delegate instanceof TransformableItemDefinition) {
// CopyOf constructor

Expand Down

0 comments on commit 475a7c8

Please sign in to comment.