Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: extract methods for parsing method input+output definitions #84

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: extract getInputDefinition
  • Loading branch information
svandriel committed May 7, 2024
commit d3157095df18d98a83df7739ec1d4c748199353d
98 changes: 58 additions & 40 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from "path";
import { ComplexTypeElement } from "soap/lib/wsdl/elements";
import { open_wsdl } from "soap/lib/wsdl/index";
import { WSDL, open_wsdl } from "soap/lib/wsdl/index";
import { Definition, Method, ParsedWsdl, Port, Service } from "./models/parsed-wsdl";
import { changeCase } from "./utils/change-case";
import { stripExtension } from "./utils/file";
@@ -287,54 +287,38 @@ export async function parseWsdl(wsdlPath: string, options: Partial<ParserOptions
Logger.debug(`Parsing Method ${methodName}`);

// TODO: Deduplicate code below by refactoring it to external function. Is it even possible ?
let requestParamName = "request";
let inputDefinition: Definition = null; // default type

if (method.input) {
if (method.input.$name) {
requestParamName = method.input.$name;
}
const inputMessage = wsdl.definitions.messages[method.input.$name];
if (inputMessage.element) {
// TODO: if `$type` not defined, inline type into function declartion (do not create definition file) - wsimport
const typeName = inputMessage.element.$type ?? inputMessage.element.$name;
const type = parsedWsdl.findDefinition(
inputMessage.element.$type ?? inputMessage.element.$name
const inputName = method.input.$name;
if (!inputName) {
throw new Error(
`Method '${serviceName}.${portName}.${methodName}' doesn't have input name`
);
inputDefinition =
type ??
parseDefinition(
parsedWsdl,
mergedOptions,
typeName,
inputMessage.parts,
[typeName],
visitedDefinitions
);
} else if (inputMessage.parts) {
const type = parsedWsdl.findDefinition(requestParamName);
inputDefinition =
type ??
parseDefinition(
parsedWsdl,
mergedOptions,
requestParamName,
inputMessage.parts,
[requestParamName],
visitedDefinitions
);
} else {
}
inputDefinition = getInputDefinition(
inputName,
wsdl,
parsedWsdl,
mergedOptions,
visitedDefinitions
);
if (!inputDefinition) {
Logger.debug(
`Method '${serviceName}.${portName}.${methodName}' doesn't have any input defined`
);
}
}

let responseParamName = "response";
let outputDefinition: Definition = null; // default type, `{}` or `unknown` ?
if (method.output) {
if (method.output.$name) {
responseParamName = method.output.$name;
if (!method.output.$name) {
throw new Error(
`Method '${serviceName}.${portName}.${methodName}' doesn't have output name`
);
}

const methodOutputName = method.output.$name;
const outputMessage = wsdl.definitions.messages[method.output.$name];
if (outputMessage.element) {
// TODO: if `$type` not defined, inline type into function declartion (do not create definition file) - wsimport
@@ -351,20 +335,21 @@ export async function parseWsdl(wsdlPath: string, options: Partial<ParserOptions
visitedDefinitions
);
} else {
const type = parsedWsdl.findDefinition(responseParamName);
const type = parsedWsdl.findDefinition(methodOutputName);
outputDefinition =
type ??
parseDefinition(
parsedWsdl,
mergedOptions,
responseParamName,
methodOutputName,
outputMessage.parts,
[responseParamName],
[methodOutputName],
visitedDefinitions
);
}
}

const requestParamName = method.input?.$name ?? "request";
const camelParamName = changeCase(requestParamName);
const portMethod: Method = {
name: methodName,
@@ -401,3 +386,36 @@ export async function parseWsdl(wsdlPath: string, options: Partial<ParserOptions
);
});
}

function getInputDefinition(
methodInputName: string,
wsdl: WSDL,
parsedWsdl: ParsedWsdl,
mergedOptions: ParserOptions,
visitedDefinitions: VisitedDefinition[]
): Definition {
const inputMessage = wsdl.definitions.messages[methodInputName];
if (inputMessage.element) {
// TODO: if `$type` not defined, inline type into function declartion (do not create definition file) - wsimport
const typeName = inputMessage.element.$type ?? inputMessage.element.$name;
const type = parsedWsdl.findDefinition(inputMessage.element.$type ?? inputMessage.element.$name);
return (
type ??
parseDefinition(parsedWsdl, mergedOptions, typeName, inputMessage.parts, [typeName], visitedDefinitions)
);
} else if (inputMessage.parts) {
const type = parsedWsdl.findDefinition(methodInputName);
return (
type ??
parseDefinition(
parsedWsdl,
mergedOptions,
methodInputName,
inputMessage.parts,
[methodInputName],
visitedDefinitions
)
);
}
return null;
}