1
1
import * as path from "path" ;
2
- import { ComplexTypeElement } from "soap/lib/wsdl/elements" ;
2
+ import {
3
+ AllElement ,
4
+ ChoiceElement ,
5
+ ComplexContentElement ,
6
+ ComplexTypeElement ,
7
+ DefinitionsElement ,
8
+ ElementElement ,
9
+ ExtensionElement ,
10
+ SequenceElement ,
11
+ SimpleContentElement ,
12
+ SimpleTypeElement ,
13
+ } from "soap/lib/wsdl/elements" ;
14
+ import { splitQName } from "soap/lib/utils" ;
3
15
import { open_wsdl } from "soap/lib/wsdl/index" ;
4
16
import { Definition , Method , ParsedWsdl , Port , Service } from "./models/parsed-wsdl" ;
5
17
import { changeCase } from "./utils/change-case" ;
@@ -12,13 +24,15 @@ interface ParserOptions {
12
24
modelNameSuffix : string ;
13
25
maxRecursiveDefinitionName : number ;
14
26
caseInsensitiveNames : boolean ;
27
+ useWsdlTypeNames : boolean ;
15
28
}
16
29
17
30
const defaultOptions : ParserOptions = {
18
31
modelNamePreffix : "" ,
19
32
modelNameSuffix : "" ,
20
33
maxRecursiveDefinitionName : 64 ,
21
34
caseInsensitiveNames : false ,
35
+ useWsdlTypeNames : false ,
22
36
} ;
23
37
24
38
type VisitedDefinition = {
@@ -53,6 +67,60 @@ function toPrimitiveType(type: string): string {
53
67
return NODE_SOAP_PARSED_TYPES [ type ] || "string" ;
54
68
}
55
69
70
+ type ElementSchema = ElementElement | ComplexTypeElement | SimpleTypeElement ;
71
+
72
+ function findElementSchemaType ( definitions : DefinitionsElement , element : ElementSchema ) {
73
+ if ( ! ( "$type" in element ) && ! ( "$ref" in element ) ) return element ;
74
+ const type = element . $type || element . $ref ;
75
+ if ( ! type ) return element ;
76
+ const { prefix, name : localName } = splitQName ( type ) ;
77
+ const ns = element . schemaXmlns [ prefix ] ?? definitions . xmlns [ prefix ] ?? definitions . xmlns [ element . targetNSAlias ] ;
78
+ const schema = definitions . schemas [ ns ] ;
79
+ if ( ! schema ) return element ;
80
+ const typeElement = schema . complexTypes [ localName ] ?? schema . types [ localName ] ;
81
+ return typeElement ;
82
+ }
83
+
84
+ function findPropSchemaType (
85
+ definitions : DefinitionsElement ,
86
+ parentElement : ElementSchema | undefined ,
87
+ propName : string
88
+ ) : ElementSchema | undefined {
89
+ if ( ! parentElement ?. children ) return undefined ;
90
+ for ( const child of parentElement . children ) {
91
+ if (
92
+ child instanceof ChoiceElement ||
93
+ child instanceof SequenceElement ||
94
+ child instanceof AllElement ||
95
+ child instanceof SimpleContentElement ||
96
+ child instanceof ComplexContentElement ||
97
+ child instanceof ExtensionElement
98
+ ) {
99
+ return findPropSchemaType ( definitions , child , propName ) ;
100
+ }
101
+ if ( child . $name === propName ) {
102
+ if (
103
+ child instanceof ElementElement ||
104
+ child instanceof ComplexTypeElement ||
105
+ child instanceof SimpleTypeElement
106
+ ) {
107
+ return findElementSchemaType ( definitions , child ) ;
108
+ }
109
+ }
110
+ }
111
+ return undefined ;
112
+ }
113
+
114
+ function getNameFromSchema ( element : ElementSchema | undefined ) {
115
+ if ( ! element ) return undefined ;
116
+ if ( "$type" in element || "$ref" in element ) {
117
+ const type = element . $type || element . $ref ;
118
+ const { name : localName } = splitQName ( type ) ;
119
+ return localName ;
120
+ }
121
+ return element . $name ;
122
+ }
123
+
56
124
/**
57
125
* parse definition
58
126
* @param parsedWsdl context of parsed wsdl
@@ -67,7 +135,9 @@ function parseDefinition(
67
135
name : string ,
68
136
defParts : { [ propNameType : string ] : any } ,
69
137
stack : string [ ] ,
70
- visitedDefs : Array < VisitedDefinition >
138
+ visitedDefs : Array < VisitedDefinition > ,
139
+ definitions : DefinitionsElement ,
140
+ schema : ElementSchema | undefined
71
141
) : Definition {
72
142
const defName = changeCase ( name , { pascalCase : true } ) ;
73
143
@@ -146,13 +216,17 @@ function parseDefinition(
146
216
} ) ;
147
217
} else {
148
218
try {
219
+ const propSchema = findPropSchemaType ( definitions , schema , stripedPropName ) ;
220
+ const guessPropName = getNameFromSchema ( propSchema ) ?? stripedPropName ;
149
221
const subDefinition = parseDefinition (
150
222
parsedWsdl ,
151
223
options ,
152
- stripedPropName ,
224
+ guessPropName ,
153
225
type ,
154
226
[ ...stack , propName ] ,
155
- visitedDefs
227
+ visitedDefs ,
228
+ definitions ,
229
+ propSchema
156
230
) ;
157
231
definition . properties . push ( {
158
232
kind : "REFERENCE" ,
@@ -205,13 +279,17 @@ function parseDefinition(
205
279
} ) ;
206
280
} else {
207
281
try {
282
+ const propSchema = findPropSchemaType ( definitions , schema , propName ) ;
283
+ const guessPropName = getNameFromSchema ( propSchema ) ?? propName ;
208
284
const subDefinition = parseDefinition (
209
285
parsedWsdl ,
210
286
options ,
211
- propName ,
287
+ guessPropName ,
212
288
type ,
213
289
[ ...stack , propName ] ,
214
- visitedDefs
290
+ visitedDefs ,
291
+ definitions ,
292
+ propSchema
215
293
) ;
216
294
definition . properties . push ( {
217
295
kind : "REFERENCE" ,
@@ -300,6 +378,9 @@ export async function parseWsdl(wsdlPath: string, options: Partial<ParserOptions
300
378
const type = parsedWsdl . findDefinition (
301
379
inputMessage . element . $type ?? inputMessage . element . $name
302
380
) ;
381
+ const schema = mergedOptions . useWsdlTypeNames
382
+ ? findElementSchemaType ( wsdl . definitions , inputMessage . element )
383
+ : undefined ;
303
384
inputDefinition =
304
385
type ??
305
386
parseDefinition (
@@ -308,7 +389,9 @@ export async function parseWsdl(wsdlPath: string, options: Partial<ParserOptions
308
389
typeName ,
309
390
inputMessage . parts ,
310
391
[ typeName ] ,
311
- visitedDefinitions
392
+ visitedDefinitions ,
393
+ wsdl . definitions ,
394
+ schema
312
395
) ;
313
396
} else if ( inputMessage . parts ) {
314
397
const type = parsedWsdl . findDefinition ( requestParamName ) ;
@@ -320,7 +403,9 @@ export async function parseWsdl(wsdlPath: string, options: Partial<ParserOptions
320
403
requestParamName ,
321
404
inputMessage . parts ,
322
405
[ requestParamName ] ,
323
- visitedDefinitions
406
+ visitedDefinitions ,
407
+ wsdl . definitions ,
408
+ undefined
324
409
) ;
325
410
} else {
326
411
Logger . debug (
@@ -340,6 +425,9 @@ export async function parseWsdl(wsdlPath: string, options: Partial<ParserOptions
340
425
// TODO: if `$type` not defined, inline type into function declartion (do not create definition file) - wsimport
341
426
const typeName = outputMessage . element . $type ?? outputMessage . element . $name ;
342
427
const type = parsedWsdl . findDefinition ( typeName ) ;
428
+ const schema = mergedOptions . useWsdlTypeNames
429
+ ? findElementSchemaType ( wsdl . definitions , outputMessage . element )
430
+ : undefined ;
343
431
outputDefinition =
344
432
type ??
345
433
parseDefinition (
@@ -348,7 +436,9 @@ export async function parseWsdl(wsdlPath: string, options: Partial<ParserOptions
348
436
typeName ,
349
437
outputMessage . parts ,
350
438
[ typeName ] ,
351
- visitedDefinitions
439
+ visitedDefinitions ,
440
+ wsdl . definitions ,
441
+ schema
352
442
) ;
353
443
} else {
354
444
const type = parsedWsdl . findDefinition ( responseParamName ) ;
@@ -360,7 +450,9 @@ export async function parseWsdl(wsdlPath: string, options: Partial<ParserOptions
360
450
responseParamName ,
361
451
outputMessage . parts ,
362
452
[ responseParamName ] ,
363
- visitedDefinitions
453
+ visitedDefinitions ,
454
+ wsdl . definitions ,
455
+ undefined
364
456
) ;
365
457
}
366
458
}
0 commit comments