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 all commits
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
170 changes: 98 additions & 72 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 { ComplexTypeElement, OperationElement } from "soap/lib/wsdl/elements";
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";
Expand Down Expand Up @@ -285,86 +285,44 @@ export async function parseWsdl(wsdlPath: string, options: Partial<ParserOptions

for (const [methodName, method] of Object.entries(port.binding.methods)) {
Logger.debug(`Parsing Method ${methodName}`);
const methodPath = `${serviceName}.${portName}.${methodName}`;

let inputDefinition: Definition | null = null; // default type

// 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 methodInputName = method.input.$name;
if (!methodInputName) {
throw new Error(`Method '${methodPath}' doesn't have an 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
);
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 {
Logger.debug(
`Method '${serviceName}.${portName}.${methodName}' doesn't have any input defined`
);
inputDefinition = createMethodInputDefinition({
methodInputName,
wsdl,
parsedWsdl,
mergedOptions,
visitedDefinitions,
});
if (!inputDefinition) {
Logger.debug(`Method '${methodPath}' doesn't have any input defined`);
}
}

let responseParamName = "response";
let outputDefinition: Definition = null; // default type, `{}` or `unknown` ?
let outputDefinition: Definition | null = null;

if (method.output) {
if (method.output.$name) {
responseParamName = 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
const typeName = outputMessage.element.$type ?? outputMessage.element.$name;
const type = parsedWsdl.findDefinition(typeName);
outputDefinition =
type ??
parseDefinition(
parsedWsdl,
mergedOptions,
typeName,
outputMessage.parts,
[typeName],
visitedDefinitions
);
} else {
const type = parsedWsdl.findDefinition(responseParamName);
outputDefinition =
type ??
parseDefinition(
parsedWsdl,
mergedOptions,
responseParamName,
outputMessage.parts,
[responseParamName],
visitedDefinitions
);
const methodOutputName = method.output.$name;
if (!methodOutputName) {
throw new Error(`Method '${methodPath}' doesn't have output name`);
}
outputDefinition = createMethodOutputDefinition({
methodOutputName,
wsdl,
parsedWsdl,
mergedOptions,
visitedDefinitions,
});
}

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

function createMethodInputDefinition(opts: {
methodInputName: string;
wsdl: WSDL;
parsedWsdl: ParsedWsdl;
mergedOptions: ParserOptions;
visitedDefinitions: VisitedDefinition[];
}): Definition | null {
const { methodInputName, wsdl, parsedWsdl, mergedOptions, visitedDefinitions } = opts;
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
)
);
} else {
return null;
}
}

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