spreeuwers/xsd2ts

error during parsing

Opened this issue · 2 comments

the library fails to parse this xsd: https://www.agenziaentrate.gov.it/portale/documents/20143/3669651/Schema_VFPR12+%281%29.xsd/c25b2a7b-a3da-2a1f-c3ac-73d39921c7f5

this is the error reported:
node_modules/xsd2ts/regexp2aliasType.js:212
var orgLength_1 = options[options.length - 1].length;
^

TypeError: Cannot read property 'length' of undefined
at _loop_1 (node_modules/xsd2ts/regexp2aliasType.js:212:59)
at variants (node_modules/xsd2ts/regexp2aliasType.js:244:23)
at option (node_modules/xsd2ts/regexp2aliasType.js:263:14)
at expression (node_modules/xsd2ts/regexp2aliasType.js:362:14)
at Object.regexpPattern2typeAlias (node_modules/xsd2ts/regexp2aliasType.js:419:14)
at node_modules/xsd2ts/classGenerator.js:230:48
at Array.forEach ()
at ClassGenerator.generateClassFileDefinition (node_modules/xsd2ts/classGenerator.js:225:14)
at Object. (.../src/....js:17:30)
at Module._compile (internal/modules/cjs/loader.js:1085:14)

I've been able to skip the error by patching the package with "patch-package", but when I tried to apply it in the source code to prepare a pull request I couldn't compile, because ts-code-generator gives error, and pretty much all packages are quite outdated...

diff --git a/node_modules/xsd2ts/regexp2aliasType.js b/node_modules/xsd2ts/regexp2aliasType.js
index a37179e..64374c5 100644
--- a/node_modules/xsd2ts/regexp2aliasType.js
+++ b/node_modules/xsd2ts/regexp2aliasType.js
@@ -209,11 +209,12 @@ function variants(pattern, index, maxLength) {diff --git a/node_modules/xsd2ts/regexp2aliasType.js b/node_modules/xsd2ts/regexp2aliasType.js
index a37179e..64374c5 100644
--- a/node_modules/xsd2ts/regexp2aliasType.js
+++ b/node_modules/xsd2ts/regexp2aliasType.js
@@ -209,11 +209,12 @@ function variants(pattern, index, maxLength) {
         if (r) {
             var _e = r.split(',').map(function (s) { return +s; }), min_1 = _e[0], max_2 = _e[1];
             options = options.map(function (o) { return o.substring(0, o.length - 1); });
-            var orgLength_1 = options[options.length - 1].length;
+            var lastOpt = options[options.length - 1]; 
+            var orgLength_1 = lastOpt ? lastOpt.length : 0;
             //options = options.map(o => o.substring(0, o.length - 1));
             var chars = result.split('');
             chars.unshift('');
-            while (options[options.length - 1].length < Math.min(maxLength, (1 + max_2)) && options.length < MAX_OPTIONS_LENGTH) {
+            while (orgLength_1 > 0 && orgLength_1 < Math.min(maxLength, (1 + max_2)) && options.length < MAX_OPTIONS_LENGTH) {
                 //console.log('    buildVariants options:', options, result, Math.min(maxLength, +max), options[options.length - 1].length, r);
                 options = buildVariants(options, chars, maxLength);
             }
         if (r) {
             var _e = r.split(',').map(function (s) { return +s; }), min_1 = _e[0], max_2 = _e[1];
             options = options.map(function (o) { return o.substring(0, o.length - 1); });
-            var orgLength_1 = options[options.length - 1].length;
+            var lastOpt = options[options.length - 1]; 
+            var orgLength_1 = lastOpt ? lastOpt.length : 0;
             //options = options.map(o => o.substring(0, o.length - 1));
             var chars = result.split('');
             chars.unshift('');
-            while (options[options.length - 1].length < Math.min(maxLength, (1 + max_2)) && options.length < MAX_OPTIONS_LENGTH) {
+            while (orgLength_1 > 0 && orgLength_1 < Math.min(maxLength, (1 + max_2)) && options.length < MAX_OPTIONS_LENGTH) {
                 //console.log('    buildVariants options:', options, result, Math.min(maxLength, +max), options[options.length - 1].length, r);
                 options = buildVariants(options, chars, maxLength);
             }