mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Removing old useUnionTypes
- Working on fix for reusable request body
This commit is contained in:
parent
ceb1aef0c4
commit
e0c2b799e4
@ -12,7 +12,7 @@ program
|
||||
.option('-o, --output <value>', 'Output directory', './generated')
|
||||
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr]', 'fetch')
|
||||
.option('--useOptions', 'Use options vs arguments style functions')
|
||||
.option('--useUnionTypes', 'Use inclusive union types')
|
||||
.option('--useUnionTypes', 'Use union types instead of enums')
|
||||
.option('--exportCore <value>', 'Generate core', true)
|
||||
.option('--exportServices <value>', 'Generate services', true)
|
||||
.option('--exportModels <value>', 'Generate models', true)
|
||||
|
||||
@ -6,7 +6,6 @@ describe('index', () => {
|
||||
input: './test/mock/v2/spec.json',
|
||||
output: './test/result/v2/',
|
||||
useOptions: true,
|
||||
useUnionTypes: true,
|
||||
write: false,
|
||||
});
|
||||
});
|
||||
@ -16,7 +15,6 @@ describe('index', () => {
|
||||
input: './test/mock/v3/spec.json',
|
||||
output: './test/result/v3/',
|
||||
useOptions: true,
|
||||
useUnionTypes: true,
|
||||
write: false,
|
||||
});
|
||||
});
|
||||
|
||||
@ -61,18 +61,18 @@ export async function generate({
|
||||
switch (openApiVersion) {
|
||||
case OpenApiVersion.V2: {
|
||||
const client = parseV2(openApi);
|
||||
const clientFinal = postProcessClient(client, useUnionTypes);
|
||||
const clientFinal = postProcessClient(client);
|
||||
if (write) {
|
||||
await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas);
|
||||
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case OpenApiVersion.V3: {
|
||||
const client = parseV3(openApi);
|
||||
const clientFinal = postProcessClient(client, useUnionTypes);
|
||||
const clientFinal = postProcessClient(client);
|
||||
if (write) {
|
||||
await writeClient(clientFinal, templates, output, httpClient, useOptions, exportCore, exportServices, exportModels, exportSchemas);
|
||||
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@ -2,13 +2,13 @@
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
import {getFormData} from './getFormData';
|
||||
import {getQueryString} from './getQueryString';
|
||||
import {OpenAPI} from './OpenAPI';
|
||||
import {RequestOptions} from './RequestOptions';
|
||||
import {requestUsingFetch} from './requestUsingFetch';
|
||||
import {requestUsingXHR} from './requestUsingXHR';
|
||||
import {Result} from './Result';
|
||||
import { getFormData } from './getFormData';
|
||||
import { getQueryString } from './getQueryString';
|
||||
import { OpenAPI } from './OpenAPI';
|
||||
import { RequestOptions } from './RequestOptions';
|
||||
import { requestUsingFetch } from './requestUsingFetch';
|
||||
import { requestUsingXHR } from './requestUsingXHR';
|
||||
import { Result } from './Result';
|
||||
|
||||
/**
|
||||
* Create the request.
|
||||
|
||||
@ -5,7 +5,6 @@ import { unique } from './unique';
|
||||
|
||||
/**
|
||||
* Get the full list of models that are extended by the given model.
|
||||
* This list is used when we have the flag "useUnionTypes" enabled.
|
||||
* @param model
|
||||
* @param client
|
||||
*/
|
||||
|
||||
@ -5,7 +5,6 @@ import { unique } from './unique';
|
||||
|
||||
/**
|
||||
* Get the full list of models that are extended from the given model.
|
||||
* This list is used when we have the flag "useUnionTypes" enabled.
|
||||
* @param model
|
||||
* @param client
|
||||
*/
|
||||
|
||||
@ -5,12 +5,11 @@ import { postProcessService } from './postProcessService';
|
||||
/**
|
||||
* Post process client
|
||||
* @param client Client object with all the models, services, etc.
|
||||
* @param useUnionTypes Use inclusive union types.
|
||||
*/
|
||||
export function postProcessClient(client: Client, useUnionTypes: boolean): Client {
|
||||
export function postProcessClient(client: Client): Client {
|
||||
return {
|
||||
...client,
|
||||
models: client.models.map(model => postProcessModel(model, client, useUnionTypes)),
|
||||
services: client.services.map(service => postProcessService(service, client, useUnionTypes)),
|
||||
models: client.models.map(model => postProcessModel(model)),
|
||||
services: client.services.map(service => postProcessService(service)),
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,24 +1,18 @@
|
||||
import { Client } from '../client/interfaces/Client';
|
||||
import { Model } from '../client/interfaces/Model';
|
||||
import { postProcessModelEnum } from './postProcessModelEnum';
|
||||
import { postProcessModelEnums } from './postProcessModelEnums';
|
||||
import { postProcessModelImports } from './postProcessModelImports';
|
||||
import { postProcessUnionTypes } from './postProcessUnionTypes';
|
||||
|
||||
/**
|
||||
* Post process the model. If needed this will convert types to union types,
|
||||
* see the "useUnionTypes" flag in the documentation. Plus this will cleanup
|
||||
* any double imports or enum values.
|
||||
* Post process the model.
|
||||
* This will cleanup any double imports or enum values.
|
||||
* @param model
|
||||
* @param client
|
||||
* @param useUnionTypes
|
||||
*/
|
||||
export function postProcessModel(model: Model, client: Client, useUnionTypes: boolean): Model {
|
||||
const clone = postProcessUnionTypes(model, client, useUnionTypes);
|
||||
export function postProcessModel(model: Model): Model {
|
||||
return {
|
||||
...clone,
|
||||
imports: postProcessModelImports(clone),
|
||||
enums: postProcessModelEnums(clone),
|
||||
enum: postProcessModelEnum(clone),
|
||||
...model,
|
||||
imports: postProcessModelImports(model),
|
||||
enums: postProcessModelEnums(model),
|
||||
enum: postProcessModelEnum(model),
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,11 +1,10 @@
|
||||
import { Client } from '../client/interfaces/Client';
|
||||
import { Service } from '../client/interfaces/Service';
|
||||
import { postProcessServiceImports } from './postProcessServiceImports';
|
||||
import { postProcessServiceOperations } from './postProcessServiceOperations';
|
||||
|
||||
export function postProcessService(service: Service, client: Client, useUnionTypes: boolean): Service {
|
||||
export function postProcessService(service: Service): Service {
|
||||
const clone = { ...service };
|
||||
clone.operations = postProcessServiceOperations(clone, client, useUnionTypes);
|
||||
clone.operations = postProcessServiceOperations(clone);
|
||||
clone.operations.forEach(operation => {
|
||||
clone.imports.push(...operation.imports);
|
||||
});
|
||||
|
||||
@ -1,10 +1,8 @@
|
||||
import { Client } from '../client/interfaces/Client';
|
||||
import { Operation } from '../client/interfaces/Operation';
|
||||
import { Service } from '../client/interfaces/Service';
|
||||
import { flatMap } from './flatMap';
|
||||
import { postProcessUnionTypes } from './postProcessUnionTypes';
|
||||
|
||||
export function postProcessServiceOperations(service: Service, client: Client, useUnionTypes: boolean = false): Operation[] {
|
||||
export function postProcessServiceOperations(service: Service): Operation[] {
|
||||
const names = new Map<string, number>();
|
||||
|
||||
return service.operations.map(operation => {
|
||||
@ -12,8 +10,6 @@ export function postProcessServiceOperations(service: Service, client: Client, u
|
||||
|
||||
// Parse the service parameters and results, very similar to how we parse
|
||||
// properties of models. These methods will extend the type if needed.
|
||||
clone.parameters = clone.parameters.map(parameter => postProcessUnionTypes(parameter, client, useUnionTypes));
|
||||
clone.results = clone.results.map(result => postProcessUnionTypes(result, client, useUnionTypes));
|
||||
clone.imports.push(...flatMap(clone.parameters, parameter => parameter.imports));
|
||||
clone.imports.push(...flatMap(clone.results, result => result.imports));
|
||||
|
||||
|
||||
@ -1,41 +0,0 @@
|
||||
import { Client } from '../client/interfaces/Client';
|
||||
import { Model } from '../client/interfaces/Model';
|
||||
import { getExtendedByList } from './getExtendedByList';
|
||||
|
||||
/**
|
||||
* This post processor will convert types to union types. For more information
|
||||
* please check the documentation. In a nutshell: By setting the "useUnionTypes"
|
||||
* flag we will convert base types to a union of types that are extended from
|
||||
* the base type.
|
||||
* @param model
|
||||
* @param client
|
||||
* @param useUnionTypes
|
||||
*/
|
||||
export function postProcessUnionTypes<T extends Model>(model: T, client: Client, useUnionTypes: boolean): T {
|
||||
const clone = { ...model };
|
||||
|
||||
if (useUnionTypes) {
|
||||
// If this is not a root definition, then new need to check the base type
|
||||
if (!clone.isDefinition) {
|
||||
const extendedBy = getExtendedByList(clone, client);
|
||||
const extendedByNames = extendedBy.map(m => m.name);
|
||||
clone.base = [clone.base, ...extendedByNames].sort().join(' | ');
|
||||
clone.imports = clone.imports.concat(...extendedByNames);
|
||||
}
|
||||
|
||||
// In any case we need to check the properties of a model.
|
||||
// When the types get extended, we also need to make sure we update the imports.
|
||||
clone.properties = clone.properties.map(property => postProcessUnionTypes(property, client, useUnionTypes));
|
||||
clone.properties.forEach(property => {
|
||||
clone.imports.push(...property.imports);
|
||||
});
|
||||
|
||||
// When the model has a link (in case of an Array or Dictionary),
|
||||
// then we also process this linked model and again update the imports.
|
||||
clone.link = clone.link ? postProcessUnionTypes(clone.link, client, useUnionTypes) : null;
|
||||
if (clone.link) {
|
||||
clone.imports.push(...clone.link.imports);
|
||||
}
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
@ -21,6 +21,7 @@ async function copySupportFile(filePath: string, outputPath: string): Promise<vo
|
||||
* @param output Directory to write the generated files to.
|
||||
* @param httpClient The selected httpClient (fetch or XHR).
|
||||
* @param useOptions Use options or arguments functions.
|
||||
* @param useUnionTypes Use union types or enums.
|
||||
* @param exportCore: Generate core.
|
||||
* @param exportServices: Generate services.
|
||||
* @param exportModels: Generate models.
|
||||
@ -32,6 +33,7 @@ export async function writeClient(
|
||||
output: string,
|
||||
httpClient: HttpClient,
|
||||
useOptions: boolean,
|
||||
useUnionTypes: boolean,
|
||||
exportCore: boolean,
|
||||
exportServices: boolean,
|
||||
exportModels: boolean,
|
||||
|
||||
@ -194,13 +194,13 @@ exports[`v2 should generate: ./test/result/v2/core/request.ts 1`] = `
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
import {getFormData} from './getFormData';
|
||||
import {getQueryString} from './getQueryString';
|
||||
import {OpenAPI} from './OpenAPI';
|
||||
import {RequestOptions} from './RequestOptions';
|
||||
import {requestUsingFetch} from './requestUsingFetch';
|
||||
import {requestUsingXHR} from './requestUsingXHR';
|
||||
import {Result} from './Result';
|
||||
import { getFormData } from './getFormData';
|
||||
import { getQueryString } from './getQueryString';
|
||||
import { OpenAPI } from './OpenAPI';
|
||||
import { RequestOptions } from './RequestOptions';
|
||||
import { requestUsingFetch } from './requestUsingFetch';
|
||||
import { requestUsingXHR } from './requestUsingXHR';
|
||||
import { Result } from './Result';
|
||||
|
||||
/**
|
||||
* Create the request.
|
||||
@ -2684,13 +2684,13 @@ exports[`v3 should generate: ./test/result/v3/core/request.ts 1`] = `
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
import {getFormData} from './getFormData';
|
||||
import {getQueryString} from './getQueryString';
|
||||
import {OpenAPI} from './OpenAPI';
|
||||
import {RequestOptions} from './RequestOptions';
|
||||
import {requestUsingFetch} from './requestUsingFetch';
|
||||
import {requestUsingXHR} from './requestUsingXHR';
|
||||
import {Result} from './Result';
|
||||
import { getFormData } from './getFormData';
|
||||
import { getQueryString } from './getQueryString';
|
||||
import { OpenAPI } from './OpenAPI';
|
||||
import { RequestOptions } from './RequestOptions';
|
||||
import { requestUsingFetch } from './requestUsingFetch';
|
||||
import { requestUsingXHR } from './requestUsingXHR';
|
||||
import { Result } from './Result';
|
||||
|
||||
/**
|
||||
* Create the request.
|
||||
@ -3057,6 +3057,7 @@ export { DuplicateService } from './services/DuplicateService';
|
||||
export { HeaderService } from './services/HeaderService';
|
||||
export { MultipartService } from './services/MultipartService';
|
||||
export { ParametersService } from './services/ParametersService';
|
||||
export { RequestBodyService } from './services/RequestBodyService';
|
||||
export { ResponseService } from './services/ResponseService';
|
||||
export { SimpleService } from './services/SimpleService';
|
||||
export { TypesService } from './services/TypesService';
|
||||
@ -4931,6 +4932,39 @@ export class ParametersService {
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`v3 should generate: ./test/result/v3/services/RequestBodyService.ts 1`] = `
|
||||
"/* istanbul ignore file */
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
import { catchGenericError } from '../core/ApiError';
|
||||
import { request as __request } from '../core/request';
|
||||
import { OpenAPI } from '../core/OpenAPI';
|
||||
|
||||
export class RequestBodyService {
|
||||
|
||||
/**
|
||||
* @param requestBody
|
||||
* @throws ApiError
|
||||
*/
|
||||
public static async postRequestBodyService(
|
||||
requestBody?: any,
|
||||
): Promise<void> {
|
||||
|
||||
const result = await __request({
|
||||
method: 'post',
|
||||
path: \`/api/v\${OpenAPI.VERSION}/requestBody/\`,
|
||||
body: requestBody,
|
||||
});
|
||||
|
||||
catchGenericError(result);
|
||||
|
||||
return result.body;
|
||||
}
|
||||
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`v3 should generate: ./test/result/v3/services/ResponseService.ts 1`] = `
|
||||
"/* istanbul ignore file */
|
||||
/* tslint:disable */
|
||||
|
||||
@ -28,7 +28,6 @@ async function run() {
|
||||
output: './test/result/v2/',
|
||||
httpClient: OpenAPI.HttpClient.FETCH,
|
||||
useOptions: false,
|
||||
useUnionTypes: false,
|
||||
exportCore: true,
|
||||
exportSchemas: true,
|
||||
exportModels: true,
|
||||
@ -40,7 +39,6 @@ async function run() {
|
||||
output: './test/result/v3/',
|
||||
httpClient: OpenAPI.HttpClient.FETCH,
|
||||
useOptions: false,
|
||||
useUnionTypes: false,
|
||||
exportCore: true,
|
||||
exportSchemas: true,
|
||||
exportModels: true,
|
||||
|
||||
@ -11,7 +11,6 @@ describe('v2', () => {
|
||||
output: './test/result/v2/',
|
||||
httpClient: OpenAPI.HttpClient.FETCH,
|
||||
useOptions: false,
|
||||
useUnionTypes: false,
|
||||
exportCore: true,
|
||||
exportSchemas: true,
|
||||
exportModels: true,
|
||||
@ -34,7 +33,6 @@ describe('v3', () => {
|
||||
output: './test/result/v3/',
|
||||
httpClient: OpenAPI.HttpClient.FETCH,
|
||||
useOptions: false,
|
||||
useUnionTypes: false,
|
||||
exportCore: true,
|
||||
exportSchemas: true,
|
||||
exportModels: true,
|
||||
|
||||
@ -298,6 +298,16 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v{api-version}/requestBody/": {
|
||||
"post": {
|
||||
"tags": [
|
||||
"RequestBody"
|
||||
],
|
||||
"requestBody": {
|
||||
"$ref": "#/components/requestBodies/SimpleRequestBody"
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v{api-version}/defaults": {
|
||||
"get": {
|
||||
"tags": [
|
||||
@ -1115,6 +1125,20 @@
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"requestBodies": {
|
||||
"SimpleRequestBody": {
|
||||
"description": "A reusable request body",
|
||||
"required": false,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"description": "Message for default response",
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ModelWithString"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"schemas": {
|
||||
"MultilineComment": {
|
||||
"description": "Testing multiline comments.\nThis must go to the next line.\n\nThis will contain a break.",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user