Skip to content

Fix for return values to match the method definition #22

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 11 additions & 6 deletions src/generator/k6Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ function _setDefaultSchemaTitle(context: ContextSpecs) {
}
}

function _generateResponseTypeDefinition(response: GetterResponse): string {
function _getResponseDataType(response: GetterResponse): string {
let responseDataType = ''

if (
response.definition.success &&
!['any', 'unknown'].includes(response.definition.success)
Expand All @@ -44,9 +43,13 @@ function _generateResponseTypeDefinition(response: GetterResponse): string {
responseDataType += 'ResponseBody'
}

return responseDataType
}

function _generateResponseTypeDefinition(response: GetterResponse): string {
return `{
response: Response
data: ${responseDataType}
data: ${_getResponseDataType(response)}
}`
}

Expand Down Expand Up @@ -233,16 +236,18 @@ const generateK6Implementation = (

const options = _getK6RequestOptions(verbOptions)

const responseDataType = _getResponseDataType(response)

return `${operationName}(\n ${toObjectString(props, 'implementation')} requestParameters?: Params): ${_generateResponseTypeDefinition(response)} {\n${bodyForm}
${urlGeneration}
const mergedRequestParameters = this._mergeRequestParameters(requestParameters || {}, this.commonRequestParameters);
const response = http.request(${options});
let data;
let data: ${responseDataType};

try {
data = response.json();
data = response.json() as unknown as ${responseDataType};
} catch {
data = response.body;
data = JSON.parse(response.body as string) as ${responseDataType};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if response.body is not a valid a JSON?

}
return {
response,
Expand Down
3 changes: 2 additions & 1 deletion tests/functional-tests/test-generator/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const commonSubstringsForAllSDK = [
'Do not edit manually',
'Service version',
'const mergedRequestParameters = this._mergeRequestParameters( requestParameters || {}, this.commonRequestParameters, );',
'try { data = response.json(); } catch { data = response.body; }',
'try { data = response.json() as unknown as',
'catch { data = JSON.parse(response.body as string) as',
'return { response, data, };',
]

Expand Down