From 233d2d310ececc3b79662d129f09003b6ad4aeb4 Mon Sep 17 00:00:00 2001
From: ebensza <bence.janos.szabo@ericsson.com>
Date: Thu, 2 Jun 2016 12:36:59 +0200
Subject: [PATCH] xsd2ttcn: fixed enumeration restriction (bug 494987)

Signed-off-by: ebensza <bence.janos.szabo@ericsson.com>
---
 ...xample_org_enumeration_restriction3_e.ttcn | 118 ++++++++++++++++++
 .../XML/XmlWorkflow/src/xmlTest.prj           |   2 +
 .../XmlWorkflow/src/xmlTest_Testcases.ttcn    |  13 ++
 .../xsd/enumeration_restriction3.xsd          |  53 ++++++++
 xsdconvert/ComplexType.cc                     |   9 ++
 xsdconvert/SimpleType.cc                      |   7 +-
 6 files changed, 200 insertions(+), 2 deletions(-)
 create mode 100644 regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_enumeration_restriction3_e.ttcn
 create mode 100644 regression_test/XML/XmlWorkflow/xsd/enumeration_restriction3.xsd

diff --git a/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_enumeration_restriction3_e.ttcn b/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_enumeration_restriction3_e.ttcn
new file mode 100644
index 000000000..c36a1d358
--- /dev/null
+++ b/regression_test/XML/XmlWorkflow/XmlTest_expectedTtcns/www_example_org_enumeration_restriction3_e.ttcn
@@ -0,0 +1,118 @@
+/*******************************************************************************
+* Copyright (c) 2000-2016 Ericsson Telecom AB
+*
+* XSD to TTCN-3 Translator version: CRL 113 200/5 R5A                       
+*
+* 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
+*
+* Contributors:
+*   Balasko, Jeno
+*   Szabo, Bence Janos
+*
+*******************************************************************************/
+//
+//  File:          www_example_org_enumeration_restriction3_e.ttcn
+//  Description:
+//  References:
+//  Rev:
+//  Prodnr:
+//  Updated:       Tue May 31 11:21:08 2015
+//  Contact:       http://ttcn.ericsson.se
+//
+////////////////////////////////////////////////////////////////////////////////
+//	Generated from file(s):
+//	- enumeration_restriction3_e.xsd
+//			/* xml version = "1.0" encoding = "UTF-8" */
+//			/* targetnamespace = "www.example.org/enumeration_restriction3/e" */
+////////////////////////////////////////////////////////////////////////////////
+//     Modification header(s):
+//-----------------------------------------------------------------------------
+//  Modified by:
+//  Modification date:
+//  Description:
+//  Modification contact:
+//------------------------------------------------------------------------------
+////////////////////////////////////////////////////////////////////////////////
+
+
+module www_example_org_enumeration_restriction3 {
+
+
+import from XSD all;
+
+
+type CType C
+with {
+  variant "element";
+};
+
+
+type enumerated CType
+{
+	b2,
+	uNKNOWN
+}
+with {
+  variant "text 'b2' as capitalized";
+  variant "text 'uNKNOWN' as capitalized";
+};
+
+
+type enumerated CTypeEnum
+{
+	b2
+}
+with {
+  variant "text 'b2' as capitalized";
+};
+
+
+type CTypeEnum C2
+with {
+  variant "element";
+};
+
+
+type enumerated Mountainbikesize
+{
+	medium,
+	small
+}
+with {
+  variant "name as uncapitalized";
+};
+
+
+type record FamilyMountainBikeSizes
+{
+	XSD.String familyMember optional,
+	Mountainbikesize base
+}
+with {
+  variant (familyMember) "attribute";
+  variant (base) "untagged";
+};
+
+
+type record ChildMountainBikeSizes
+{
+	XSD.String familyMember optional,
+	enumerated {
+		small
+	} base
+}
+with {
+  variant (familyMember) "attribute";
+  variant (base) "untagged";
+};
+
+
+}
+with {
+  encode "XML";
+  variant "namespace as 'www.example.org/enumeration_restriction3' prefix 'nse'";
+  variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'";
+}
diff --git a/regression_test/XML/XmlWorkflow/src/xmlTest.prj b/regression_test/XML/XmlWorkflow/src/xmlTest.prj
index a4d477c0c..080be3e59 100644
--- a/regression_test/XML/XmlWorkflow/src/xmlTest.prj
+++ b/regression_test/XML/XmlWorkflow/src/xmlTest.prj
@@ -163,6 +163,7 @@
 		<File path="../xsd/including_module.xsd" />
 		<File path="../xsd/included_without_namespace.xsd" />
 		<File path="../xsd/list_complextype.xsd" />
+		<File path="../xsd/enumeration_restriction3.xsd" />
             </File_Group>
             <File_Group name="XmlTest_xsds" >
                 <File path="../XmlTest_xsds/XmlTest_boolean.xsd" />
@@ -396,6 +397,7 @@
 	        <File path="../XmlTest_expectedTtcns/www_example_org_attribute_enumeration_variant_e.ttcn" />
 	        <File path="../XmlTest_expectedTtcns/www_example_org_including_module_e.ttcn" />
 	        <File path="../XmlTest_expectedTtcns/www_example_org_list_complextype_e.ttcn" />
+	        <File path="../XmlTest_expectedTtcns/www_example_org_enumeration_restriction3_e.ttcn" />
             </File_Group>
             <File_Group name="XmlTest_src" >
                 <File path="xmlTest_Shell.ttcn" />
diff --git a/regression_test/XML/XmlWorkflow/src/xmlTest_Testcases.ttcn b/regression_test/XML/XmlWorkflow/src/xmlTest_Testcases.ttcn
index 164b88872..f78a24b2b 100644
--- a/regression_test/XML/XmlWorkflow/src/xmlTest_Testcases.ttcn
+++ b/regression_test/XML/XmlWorkflow/src/xmlTest_Testcases.ttcn
@@ -1130,6 +1130,18 @@ group UnionTest {
     }
   }
 
+ testcase tc_enumeration_restriction3() runs on xmlTest_CT {
+
+    f_shellCommandWithVerdict("xsd2ttcn  enumeration_restriction3.xsd","",c_shell_successWithoutWarningAndError)
+
+    if(getverdict==pass) {
+      f_compareFiles(
+        "www_example_org_enumeration_restriction3_e.ttcn",
+        "www_example_org_enumeration_restriction3.ttcn", c_numOfDiff_headerAndModuleName);
+    }
+  }
+
+
  testcase tc_enumeration_remove_dup() runs on xmlTest_CT {
 
     f_shellCommandWithVerdict("xsd2ttcn  enumeration_remove_dup.xsd","",c_shell_successWithoutWarningAndError)
@@ -2599,6 +2611,7 @@ control {
   execute(tc_union_optional());//CR_TR18883
   execute(tc_enumeration_union_restriction());
   execute(tc_enumeration_union_restriction2());
+  execute(tc_enumeration_restriction3());
   execute(tc_enumeration_remove_dup());
   //===complex===
   execute(tc_complex1()); //Passed
diff --git a/regression_test/XML/XmlWorkflow/xsd/enumeration_restriction3.xsd b/regression_test/XML/XmlWorkflow/xsd/enumeration_restriction3.xsd
new file mode 100644
index 000000000..8b2e64981
--- /dev/null
+++ b/regression_test/XML/XmlWorkflow/xsd/enumeration_restriction3.xsd
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema"
+    targetNamespace="www.example.org/enumeration_restriction3"
+    xmlns:nse="www.example.org/enumeration_restriction3">
+
+<element name="C">
+	<simpleType>
+		<restriction base="nse:CType" />
+	</simpleType>
+</element>
+
+<simpleType name="CType">
+	<restriction base="string">
+		<maxLength value="32" />
+		<enumeration value="B2" />
+		<enumeration value="UNKNOWN" />
+	</restriction>
+</simpleType>
+
+<simpleType name="CTypeEnum">
+	<restriction base="nse:CType" >
+		<enumeration value="B2" />
+	</restriction>
+</simpleType>
+
+<element name="C2" type="nse:CTypeEnum"/>
+
+
+<simpleType name="mountainbikesize">
+	<restriction base="string">
+		<enumeration value="small"/>
+		<enumeration value="medium"/>
+	</restriction>
+</simpleType>
+
+<complexType name="FamilyMountainBikeSizes">
+	<simpleContent>
+		<extension base="nse:mountainbikesize">
+			<attribute name="familyMember" type="string" />
+		</extension>
+	</simpleContent>
+</complexType>
+
+<complexType name="ChildMountainBikeSizes">
+	<simpleContent>
+		<restriction base="nse:FamilyMountainBikeSizes" >
+			<enumeration value="small"/>
+		</restriction>
+	</simpleContent>
+</complexType>
+
+
+</schema>
diff --git a/xsdconvert/ComplexType.cc b/xsdconvert/ComplexType.cc
index 74cac0661..8367f2a01 100644
--- a/xsdconvert/ComplexType.cc
+++ b/xsdconvert/ComplexType.cc
@@ -1782,6 +1782,15 @@ void ComplexType::resolveSimpleTypeExtension() {
         st->addToNameDepList(basefield);
         basefield->nameDep = st;
         addNameSpaceAsVariant(basefield, st);
+        const Mstring old_type = basefield->getType().originalValueWoPrefix;
+        basefield->applyReference(*st);
+        // If st has enumeration then the type is restored to the original value
+        // because enumerations cannot be extended here and this way we just
+        // we just create an alias.
+        if (st->getEnumeration().modified) {
+          basefield->setTypeValue(old_type);
+          basefield->getEnumeration().modified = false;
+        }
       }
     } else if(!isBuiltInType(basefield->getType().convertedValue)){
          printError(module->getSchemaname(), name.convertedValue,
diff --git a/xsdconvert/SimpleType.cc b/xsdconvert/SimpleType.cc
index 85fbf8148..6f52bc244 100644
--- a/xsdconvert/SimpleType.cc
+++ b/xsdconvert/SimpleType.cc
@@ -1149,8 +1149,11 @@ EnumerationType::EnumerationType(SimpleType * a_simpleType)
 
 void EnumerationType::applyReference(const EnumerationType & other) {
   if (!modified) modified = other.modified;
-  for (List<Mstring>::iterator facet = other.facets.begin(); facet; facet = facet->Next) {
-    facets.push_back(facet->Data);
+  if ((other.parent->getXsdtype() == n_NOTSET && parent->getMode() != SimpleType::restrictionMode)
+        || parent->getXsdtype() == n_simpleType) {
+    for (List<Mstring>::iterator facet = other.facets.begin(); facet; facet = facet->Next) {
+      facets.push_back(facet->Data);
+    }
   }
 }
 
-- 
GitLab