摘要 我正在使用swagger-codegen-js根据外部api定义的swagger.json生成typescript文件。
使用的包 “swagger-js-codegen”:“^ 1.12.0”,
据称问题
生成的TS文件上方法listMovies
上的返回类型只是Promise<Request.Response>
而不是Promise<Array<Movie>>
,我确实期望它是电影数组,因为响应清楚地表明了架构和思想/假设将被翻译。
给出json沿着以下几行,
"/movies": {
"get": {
"description": "Lists movies",
"operationId": "listMovies",
"responses": {
"200": {
"description": "Movie",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Movie"
}
}
},
"default": {
"$ref": "#/responses/genericError"
}
}
},
"definitions": {
"Movie": {
"description": "something",
"type": "object",
"properties": {
"archivedAt": {
"description": "When the movie was archived",
"type": "string",
"format": "nullable-date-time",
"x-go-name": "ArchivedAt",
"readOnly": true
}
}
}
生成的TS方法
/**
* Lists movies
* @method
* @name Api#listMovies
*/
listMovies(parameters: {
$queryParameters ? : any,
$domain ? : string
}): Promise <request.Response> {
const domain = parameters.$domain ? parameters.$domain : this.domain;
let path = '/movies';
.
.
.
this.request('GET', domain + path, body, headers, queryParameters, form, reject, resolve);
});
}
我用来生成上述ts文件的脚本直接来自github示例
const generateTSFilesUsingSwaggerJsCodegen = function () {
var fs = require('fs');
var CodeGen = require('swagger-js-codegen').CodeGen;
var file = 'build/sample.json';
var swagger = JSON.parse(fs.readFileSync(file, 'UTF-8'));
var tsSourceCode = CodeGen.getTypescriptCode({ className: 'Api', swagger: swagger, imports: ['../../typings/tsd.d.ts'] });
fs.writeFileSync('src/api/api.ts', tsSourceCode)
}
我在传递的连线/选项上遗漏了什么,或者这是给定json文件的预期生成文件,我需要编写自定义脚本来获得我想要的东西?
答案 0 :(得分:2)
只能编辑codegen来改变它。
但你可以使用返回值的正文
<restMethod>(<paramters>).then(respose: <request.Response>) {
let responseObject: Array<ListMovies> = response.body as Array<ListMovies>;
...
}
如果你想改编codegen,请从git中取出它并更改以下文件:
lib / codegen.js:
var getViewForSwagger2 = function(opts, type){
var swagger = opts.swagger;
var methods = [];
var authorizedMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'COPY', 'HEAD', 'OPTIONS', 'LINK', 'UNLIK', 'PURGE', 'LOCK', 'UNLOCK', 'PROPFIND'];
var data = {
isNode: type === 'node' || type === 'react',
isES6: opts.isES6 || type === 'react',
description: swagger.info.description,
isSecure: swagger.securityDefinitions !== undefined,
moduleName: opts.moduleName,
className: opts.className,
imports: opts.imports,
domain: (swagger.schemes && swagger.schemes.length > 0 && swagger.host && swagger.basePath) ? swagger.schemes[0] + '://' + swagger.host + swagger.basePath.replace(/\/+$/g,'') : '',
methods: [],
definitions: []
};
_.forEach(swagger.definitions, function(definition, name){
data.definitions.push({
name: name,
description: definition.description,
tsType: ts.convertType(definition, swagger)
});
});
最后一个_.forEach从方法的底部移到这里。
var method = {
path: path,
className: opts.className,
methodName: methodName,
method: M,
isGET: M === 'GET',
isPOST: M === 'POST',
summary: op.description || op.summary,
externalDocs: op.externalDocs,
isSecure: swagger.security !== undefined || op.security !== undefined,
isSecureToken: secureTypes.indexOf('oauth2') !== -1,
isSecureApiKey: secureTypes.indexOf('apiKey') !== -1,
isSecureBasic: secureTypes.indexOf('basic') !== -1,
parameters: [],
responseSchema: {},
headers: []
};
if (op.responses && op.responses["200"]) {
method.responseSchema = ts.convertType(op.responses["200"], swagger);
} else {
method.responseSchema = {
"tsType": "any"
}
}
这是从第102行开始。将responseSchema添加到方法并附加if / else
templates / typescript-class.mustache(第68行)
resolve(response.body);
templates / typescript-method.mustache(第69行)
}): Promise<{{#responseSchema.isRef}}{{responseSchema.target}}{{/responseSchema.isRef}}{{^responseSchema.isRef}}{{responseSchema.tsType}}{{/responseSchema.isRef}}{{#responseSchema.isArray}}<{{responseSchema.elementType.target}}>{{/responseSchema.isArray}}> {
这仅适用于简单类型,对象类型和简单/对象类型的数组。我没有实现枚举类型。
答案 1 :(得分:0)