Skip to content

Commit

Permalink
Fix same-local-names skips in JSON/YAML (MID-5326)
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed May 4, 2019
1 parent 1433e45 commit 40ef3f0
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 5 deletions.
Expand Up @@ -627,7 +627,7 @@ private Element serializeToElement(XNodeImpl xnode, QName elementName) throws Sc
return serializeToElement(xlist.get(0), elementName);
}
} else {
throw new IllegalArgumentException("Cannot serialized "+xnode+" to element");
throw new IllegalArgumentException("Cannot serialize "+xnode+" to element");
}
}

Expand Down
Expand Up @@ -410,7 +410,9 @@ private XNodeImpl parseJsonObject(JsonParsingContext ctx, IterativeParsingContex
wrappedValue = valueXNode;
}
} else {
Map.Entry<QName, XNodeImpl> entry = map.putReturningEntry(currentFieldNameInfo.name, valueXNode);
// We must NOT remove previous values because of potential unqualified value conflict (see MID-5326).
// TODO However, we should do something to deduplicate keys in maps - probably. (Current behavior is that their values are being merged.)
Map.Entry<QName, XNodeImpl> entry = map.putReturningEntry(currentFieldNameInfo.name, valueXNode, true);
if (currentFieldNameInfo.explicitEmptyNamespace) {
ctx.noNamespaceEntries.put(entry, null);
}
Expand Down
Expand Up @@ -91,8 +91,10 @@ public XNodeImpl put(QName key, XNodeImpl value) {
return previous;
}

public Entry putReturningEntry(QName key, XNodeImpl value) {
removeEntry(key);
public Entry putReturningEntry(QName key, XNodeImpl value, boolean doNotRemovePrevious) {
if (!doNotRemovePrevious) {
removeEntry(key);
}
Entry e = new Entry(key, value);
subnodes.add(e);
return e;
Expand All @@ -115,6 +117,7 @@ public void clear() {
subnodes.clear();
}

@NotNull
public Set<QName> keySet() {
Set<QName> keySet = new HashSet<>();
for (Entry entry: subnodes) {
Expand All @@ -123,6 +126,7 @@ public Set<QName> keySet() {
return keySet;
}

@NotNull
public Collection<XNodeImpl> values() {
Collection<XNodeImpl> values = new ArrayList<>(subnodes.size());
for (Entry entry: subnodes) {
Expand Down
Expand Up @@ -106,6 +106,8 @@ public class PrismInternalTestUtil implements PrismContextFactory {
public static final String USER_WILL_FILE_BASENAME = "user-will";
public static final File USER_WILL_FILE = new File(COMMON_DIR_XML, "user-will.xml");

public static final String ACCOUNT_BARBOSSA_FILE_BASENAME = "account-barbossa";

public static final String USER_ELISABETH_FILE_BASENAME = "user-elisabeth";

public static final String RESOURCE_RUM_FILE_BASENAME = "resource-rum";
Expand Down
Expand Up @@ -24,13 +24,19 @@
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.foo.AccountType;
import com.evolveum.midpoint.prism.impl.PrismReferenceValueImpl;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.path.ItemPath;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -410,6 +416,37 @@ public void test500UserElisabethRoundTrip() throws Exception {

}

// MID-5326
@Test
public void test600AccountBarbossa() throws Exception {
final String TEST_NAME = "test600AccountBarbossa";
PrismInternalTestUtil.displayTestTitle(TEST_NAME);

// GIVEN
PrismContext prismContext = constructInitializedPrismContext();

// WHEN
PrismObject<AccountType> account = prismContext.parseObject(getFile(ACCOUNT_BARBOSSA_FILE_BASENAME));

// THEN
assertNotNull(account);
System.out.println("Account:");
System.out.println(account.debugDump());

PrismContainer<?> attributes = account.findContainer(AccountType.F_ATTRIBUTES);
assertNotNull(attributes);
PrismContainerValue<?> attributesValue = attributes.getValue();
assertNotNull(attributesValue);
assertEquals("Wrong # of attributes", 3, attributesValue.getItems().size());
Set<ItemName> names = attributesValue.getItems().stream().map(a -> a.getElementName()).collect(Collectors.toSet());
assertEquals("Wrong attribute names", new HashSet<>(Arrays.asList(
new ItemName("http://midpoint.evolveum.com/xml/ns/public/resource/instance/10000000-0000-0000-0000-000000000003", "uid"),
new ItemName("http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3", "uid"),
new ItemName("http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3", "name")
)), names);
}


protected void assertUserAdhoc(PrismObject<UserType> user, boolean expectRawInConstructions) throws SchemaException {
user.checkConsistence();
assertUserJackContent(user, expectRawInConstructions, true);
Expand Down
Expand Up @@ -24,6 +24,8 @@

package com.evolveum.midpoint.prism.foo;

import com.evolveum.midpoint.prism.path.ItemName;

import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
Expand Down Expand Up @@ -58,6 +60,7 @@ public class AccountType
implements Serializable
{

public final static ItemName F_ATTRIBUTES = new ItemName(NS_FOO, "attributes");
private final static long serialVersionUID = 201202081233L;
protected AttributesType attributes;

Expand Down
@@ -0,0 +1,13 @@
{
"@ns" : "http://midpoint.evolveum.com/xml/ns/test/foo-1.xsd",
"account" : {
"oid" : "94a0ee25-262f-4c49-ad5c-567418a5f5f3",
"name" : "hbarbossa",
"attributes" : {
"@ns" : "http://midpoint.evolveum.com/xml/ns/public/resource/instance/10000000-0000-0000-0000-000000000003",
"http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3#name" : "uid=hbarbossa,ou=people,dc=example,dc=com",
"uid" : "hbarbossa",
"http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3#uid" : "8daaeeae-f0c7-41c9-b258-2a3351aa8876"
}
}
}
@@ -0,0 +1,26 @@
<!--
~ Copyright (c) 2010-2019 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<account xmlns="http://midpoint.evolveum.com/xml/ns/test/foo-1.xsd"
xmlns:icfs="http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3"
oid="94a0ee25-262f-4c49-ad5c-567418a5f5f3">
<name>hbarbossa</name>
<attributes xmlns:gen511="http://midpoint.evolveum.com/xml/ns/public/resource/instance/10000000-0000-0000-0000-000000000003">
<icfs:name>uid=hbarbossa,ou=people,dc=example,dc=com</icfs:name>
<gen511:uid>hbarbossa</gen511:uid>
<icfs:uid>8daaeeae-f0c7-41c9-b258-2a3351aa8876</icfs:uid>
</attributes>
</account>
@@ -0,0 +1,10 @@
---
'@ns': "http://midpoint.evolveum.com/xml/ns/test/foo-1.xsd"
account:
oid: "94a0ee25-262f-4c49-ad5c-567418a5f5f3"
name: "hbarbossa"
attributes:
'@ns': "http://midpoint.evolveum.com/xml/ns/public/connector/icf-1/resource-schema-3"
name: "uid=hbarbossa,ou=people,dc=example,dc=com"
http://midpoint.evolveum.com/xml/ns/public/resource/instance/10000000-0000-0000-0000-000000000003#uid: "hbarbossa"
uid: "8daaeeae-f0c7-41c9-b258-2a3351aa8876"
Expand Up @@ -313,7 +313,6 @@ public void addUserWithAssignmentExtension() throws Exception {
@Test
public void addGetFullAccount() throws Exception {
LOGGER.info("===[ addGetFullAccount ]===");
File file = new File(FOLDER_BASIC, "account-full.xml");
PrismObject<ShadowType> fileAccount = prismContext.parseObject(new File(FOLDER_BASIC, "account-full.xml"));

// apply appropriate schema
Expand Down

0 comments on commit 40ef3f0

Please sign in to comment.