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

Fix case where query parameters are not added #61

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
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
36 changes: 18 additions & 18 deletions src/openApi/v3/parser/getOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ export const getOperation = (
mediaType: null,
};

if (op.requestBody) {
const requestBodyDef = getRef<OpenApiRequestBody>(openApi, op.requestBody);
const requestBody = getOperationRequestBody(openApi, requestBodyDef);
operation.imports.push(...requestBody.imports);
operation.parametersBody = requestBody;
dataParameter.properties.push(...requestBody.properties);
if (requestBody.export === 'array') {
// if the requestBody is an array, there are no properties to showcase. We instead want to
// use the whole model as the parameter
dataParameter.properties.push(requestBody);
} else if (requestBody.export === 'one-of' || requestBody.export === 'all-of') {
// if the requestBody is a one-of/all-of, the properties cannot be used since they have no
// names. Instead we want to use the whole model as the parameter.
dataParameter.properties = [requestBody];
}
dataParameter.isRequired = requestBody.isRequired ? true : dataParameter.isRequired;
}

// Parse the operation parameters (path, query, body, etc).
if (op.parameters) {
const parameters = getOperationParameters(openApi, op.parameters);
Expand All @@ -99,24 +117,6 @@ export const getOperation = (
operation.parametersBody = parameters.parametersBody;
}

if (op.requestBody) {
const requestBodyDef = getRef<OpenApiRequestBody>(openApi, op.requestBody);
const requestBody = getOperationRequestBody(openApi, requestBodyDef);
operation.imports.push(...requestBody.imports);
operation.parametersBody = requestBody;
dataParameter.properties.push(...requestBody.properties);
if (requestBody.export === 'array') {
// if the requestBody is an array, there are no properties to showcase. We instead want to
// use the whole model as the parameter
dataParameter.properties.push(requestBody);
} else if (requestBody.export === 'one-of' || requestBody.export === 'all-of') {
// if the requestBody is a one-of/all-of, the properties cannot be used since they have no
// names. Instead we want to use the whole model as the parameter.
dataParameter.properties = [requestBody];
}
dataParameter.isRequired = requestBody.isRequired ? true : dataParameter.isRequired;
}

// Parse the operation responses.
if (op.responses) {
const operationResponses = getOperationResponses(openApi, op.responses);
Expand Down
66 changes: 66 additions & 0 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5269,6 +5269,7 @@ export { MultipleTags1Service } from './services/MultipleTags1Service.js';
export { MultipleTags2Service } from './services/MultipleTags2Service.js';
export { MultipleTags3Service } from './services/MultipleTags3Service.js';
export { NoContentService } from './services/NoContentService.js';
export { OneOfService } from './services/OneOfService.js';
export { ParametersService } from './services/ParametersService.js';
export { RequestBodyService } from './services/RequestBodyService.js';
export { ResponseService } from './services/ResponseService.js';
Expand Down Expand Up @@ -5297,6 +5298,7 @@ import { MultipleTags1Service } from './services/MultipleTags1Service.js';
import { MultipleTags2Service } from './services/MultipleTags2Service.js';
import { MultipleTags3Service } from './services/MultipleTags3Service.js';
import { NoContentService } from './services/NoContentService.js';
import { OneOfService } from './services/OneOfService.js';
import { ParametersService } from './services/ParametersService.js';
import { RequestBodyService } from './services/RequestBodyService.js';
import { ResponseService } from './services/ResponseService.js';
Expand Down Expand Up @@ -5378,6 +5380,7 @@ applyMixins(LuneClient, [
MultipleTags2Service,
MultipleTags3Service,
NoContentService,
OneOfService,
ParametersService,
RequestBodyService,
ResponseService,
Expand All @@ -5402,6 +5405,7 @@ export interface LuneClient extends
MultipleTags2Service,
MultipleTags3Service,
NoContentService,
OneOfService,
ParametersService,
RequestBodyService,
ResponseService,
Expand Down Expand Up @@ -5493,6 +5497,7 @@ export { MultipleTags1Service } from './services/MultipleTags1Service.js';
export { MultipleTags2Service } from './services/MultipleTags2Service.js';
export { MultipleTags3Service } from './services/MultipleTags3Service.js';
export { NoContentService } from './services/NoContentService.js';
export { OneOfService } from './services/OneOfService.js';
export { ParametersService } from './services/ParametersService.js';
export { RequestBodyService } from './services/RequestBodyService.js';
export { ResponseService } from './services/ResponseService.js';
Expand Down Expand Up @@ -10093,6 +10098,67 @@ export abstract class NoContentService {
}"
`;

exports[`v3 should generate: ./test/generated/v3/services/OneOfService.ts 1`] = `
"// =========================================================================================
//
// This file is AUTO-GENERATED by https://github.com/lune-climate/openapi-typescript-codegen
//
// In most cases you DON'T WANT TO MAKE MANUAL CHANGES to it – they WILL BE OVERWRITTEN.
//
// =========================================================================================

/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { ModelWithArray } from '../models/ModelWithArray.js';
import type { ModelWithDictionary } from '../models/ModelWithDictionary.js';
import type { ModelWithEnum } from '../models/ModelWithEnum.js';
import type { ModelWithString } from '../models/ModelWithString.js';

import { ClientConfig } from '../core/ClientConfig.js'
import { request as __request } from '../core/request.js'
import { ApiError } from '../core/ApiError.js'
import { AxiosInstance } from 'axios'
import { Result } from 'ts-results-es'

export abstract class OneOfService {
protected abstract client: AxiosInstance
protected abstract config: ClientConfig

/**
* @param data Request data
* @param options Additional operation options
*/
public queryParamsWithOneOf(
data?: {
/**
* Testing allOf request body at the root level
*/
modelWithOneOfRoot: (ModelWithString | ModelWithEnum | ModelWithArray | ModelWithDictionary);
/**
* This is a reusable parameter
*/
parameter?: string;
},
options?: {
/**
* Account Id to be used to perform the API call
*/
accountId?: string;
},
): Promise<Result<void, ApiError>> {
return __request(this.client, this.config, options || {}, {
method: 'POST',
url: '/api/v{api-version}/queryParamsWithOneOf',
query: {
'parameter': data?.parameter,
},
});
}

}"
`;

exports[`v3 should generate: ./test/generated/v3/services/ParametersService.ts 1`] = `
"// =========================================================================================
//
Expand Down
23 changes: 23 additions & 0 deletions test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,29 @@
}
}
}
},
"/api/v{api-version}/queryParamsWithOneOf": {
"post": {
"tags": ["OneOf"],
"parameters": [
{
"$ref": "#/components/parameters/SimpleParameter"
}
],
"operationId": "QueryParamsWithOneOf",
"requestBody": {
"description": "Testing allOf request body at the root level",
"required": true,
"nullable": false,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ModelWithOneOfRoot"
}
}
}
}
}
}
},
"components": {
Expand Down
Loading