}
}
- targetCtx.addEffectiveSubstatement(stmtCtxToBeAdded.createCopy(targetCtx, CopyType.ORIGINAL));
+ copyStatement(stmtCtxToBeAdded, targetCtx);
}
private static void performDeviateReplace(final StatementContextBase<?, ?, ?> deviateStmtCtx,
for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.effectiveSubstatements()) {
if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) {
targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeReplaced);
- targetCtx.addEffectiveSubstatement(stmtCtxToBeReplaced.createCopy(targetCtx, CopyType.ORIGINAL));
+ copyStatement(stmtCtxToBeReplaced, targetCtx);
return;
}
}
for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) {
targetCtxSubstatement.setIsSupportedToBuildEffective(false);
- targetCtx.addEffectiveSubstatement(stmtCtxToBeReplaced.createCopy(targetCtx, CopyType.ORIGINAL));
+ copyStatement(stmtCtxToBeReplaced, targetCtx);
return;
}
}
targetCtx.getStatementArgument(), stmtCtxToBeDeleted.getStatementSourceReference());
}
+ private static void copyStatement(final Mutable<?, ?, ?> stmtCtxToBeCopied,
+ final StatementContextBase<?, ?, ?> targetCtx) {
+ // we need to make a copy of the statement context only if it is an unknown statement, otherwise
+ // we can reuse the original statement context
+ if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeCopied)) {
+ targetCtx.addEffectiveSubstatement(stmtCtxToBeCopied);
+ } else {
+ targetCtx.addEffectiveSubstatement(stmtCtxToBeCopied.createCopy(targetCtx, CopyType.ORIGINAL));
+ }
+ }
+
private static boolean statementsAreEqual(final StatementDefinition firstStmtDef, final String firstStmtArg,
final StatementDefinition secondStmtDef, final String secondStmtArg) {
return firstStmtDef.equals(secondStmtDef) && Objects.equals(firstStmtArg, secondStmtArg);
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.yangtools.yang.stmt;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+
+import java.util.Date;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.model.api.Deviation;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+
+public class Bug9242Test {
+
+ @Test
+ public void testDeviateReplaceWithUserDefinedTypes() throws Exception {
+ final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/bugs/bug9242/");
+ assertNotNull(schemaContext);
+
+ final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2017-10-13");
+
+ final Module rootModule = schemaContext.findModuleByName("root-module", revision);
+ final Module impModule = schemaContext.findModuleByName("imp-module", revision);
+ assertNotNull(impModule);
+
+ TypeDefinition<?> deviatedMyLeafType = null;
+ TypeDefinition<?> deviatedMyLeaf2Type = null;
+
+ for (final Deviation deviation : rootModule.getDeviations()) {
+ if (deviation.getTargetPath().getLastComponent().equals(QName.create(
+ impModule.getQNameModule(), "my-leaf"))) {
+ deviatedMyLeafType = deviation.getDeviates().iterator().next().getDeviatedType();
+ }
+
+ if (deviation.getTargetPath().getLastComponent().equals(QName.create(
+ impModule.getQNameModule(), "my-leaf-2"))) {
+ deviatedMyLeaf2Type = deviation.getDeviates().iterator().next().getDeviatedType();
+ }
+ }
+
+ assertNotNull(deviatedMyLeafType);
+ assertNotNull(deviatedMyLeaf2Type);
+
+ final LeafSchemaNode myLeaf = (LeafSchemaNode) impModule.getDataChildByName(QName.create(
+ impModule.getQNameModule(), "my-leaf"));
+ assertSame(deviatedMyLeafType, myLeaf.getType());
+
+ final LeafSchemaNode myLeaf2 = (LeafSchemaNode) impModule.getDataChildByName(QName.create(
+ impModule.getQNameModule(), "my-leaf-2"));
+ assertSame(deviatedMyLeaf2Type, myLeaf2.getType());
+ }
+}