-
Notifications
You must be signed in to change notification settings - Fork 52
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/enhance support for "additionalProperties" #86
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -738,6 +738,8 @@ export type Order = { | |
'shipDate' ? : string; | ||
'status' ? : \\"placed\\" | \\"approved\\" | \\"delivered\\"; | ||
'complete' ? : boolean; | ||
} & { | ||
[key: string]: any; | ||
}; | ||
|
||
export type User = { | ||
|
@@ -749,16 +751,22 @@ export type User = { | |
'password' ? : string; | ||
'phone' ? : string; | ||
'userStatus' ? : number; | ||
} & { | ||
[key: string]: any; | ||
}; | ||
|
||
export type Category = { | ||
'id' ? : number; | ||
'name' ? : string; | ||
} & { | ||
[key: string]: any; | ||
}; | ||
|
||
export type Tag = { | ||
'id' ? : number; | ||
'name' ? : string; | ||
} & { | ||
[key: string]: any; | ||
}; | ||
|
||
export type Pet = { | ||
|
@@ -770,12 +778,16 @@ export type Pet = { | |
'tags' ? : Array < Tag > | ||
; | ||
'status' ? : \\"available\\" | \\"pending\\" | \\"sold\\"; | ||
} & { | ||
[key: string]: any; | ||
}; | ||
|
||
export type ApiResponse = { | ||
'code': number; | ||
'type': string; | ||
'message' ? : string; | ||
} & { | ||
[key: string]: any; | ||
}; | ||
|
||
export type Logger = { | ||
|
@@ -1425,9 +1437,7 @@ export class PetshopApi { | |
* @method | ||
* @name PetshopApi#getInventory | ||
*/ | ||
getInventory(parameters: {} & CommonRequestOptions): Promise < ResponseWithBody < 200, { | ||
[key: string]: number | ||
} >> { | ||
getInventory(parameters: {} & CommonRequestOptions): Promise < ResponseWithBody < 200, object >> { | ||
const domain = parameters.$domain ? parameters.$domain : this.domain; | ||
let path = '/store/inventory'; | ||
if (parameters.$path) { | ||
|
@@ -2464,6 +2474,270 @@ export class UsersApi { | |
export default UsersApi;" | ||
`; | ||
|
||
exports[`Should resolve "additionalProperties" 1`] = ` | ||
"// tslint:disable | ||
|
||
import * as request from \\"superagent\\"; | ||
import { | ||
SuperAgentStatic, | ||
SuperAgentRequest, | ||
Response | ||
} from \\"superagent\\"; | ||
|
||
export type RequestHeaders = { | ||
[header: string]: string; | ||
} | ||
export type RequestHeadersHandler = (headers: RequestHeaders) => RequestHeaders; | ||
|
||
export type ConfigureAgentHandler = (agent: SuperAgentStatic) => SuperAgentStatic; | ||
|
||
export type ConfigureRequestHandler = (agent: SuperAgentRequest) => SuperAgentRequest; | ||
|
||
export type CallbackHandler = (err: any, res ? : request.Response) => void; | ||
|
||
export type some_def = { | ||
'some_def' ? : string; | ||
}; | ||
|
||
export type test_add_props_01 = { | ||
'some_prop' ? : string; | ||
} & { | ||
[key: string]: any; | ||
}; | ||
|
||
export type test_add_props_02 = { | ||
'some_prop' ? : string; | ||
}; | ||
|
||
export type test_add_props_03 = { | ||
'some_prop' ? : string; | ||
} & { | ||
[key: string]: any; | ||
}; | ||
|
||
export type test_add_props_04 = { | ||
'some_prop' ? : string; | ||
} & { | ||
[key: string]: string; | ||
}; | ||
|
||
export type test_add_props_05 = { | ||
'some_prop' ? : string; | ||
} & { | ||
[key: string]: { | ||
'nested_prop' ? : string; | ||
} & { | ||
[key: string]: any; | ||
}; | ||
}; | ||
|
||
export type test_add_props_06 = { | ||
'some_prop' ? : string; | ||
} & { | ||
[key: string]: some_def; | ||
}; | ||
|
||
export type test_add_props_07 = {} & { | ||
[key: string]: any; | ||
}; | ||
|
||
export type test_add_props_08 = {}; | ||
|
||
export type test_add_props_09 = {} & { | ||
[key: string]: any; | ||
}; | ||
|
||
export type test_add_props_10 = {} & { | ||
[key: string]: string; | ||
}; | ||
|
||
export type test_add_props_11 = {} & { | ||
[key: string]: { | ||
'nested_prop' ? : string; | ||
} & { | ||
[key: string]: any; | ||
}; | ||
}; | ||
|
||
export type test_add_props_12 = {} & { | ||
[key: string]: some_def; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the types |
||
|
||
export type Logger = { | ||
log: (line: string) => any | ||
}; | ||
|
||
export interface ResponseWithBody < S extends number, T > extends Response { | ||
status: S; | ||
body: T; | ||
} | ||
|
||
export type QueryParameters = { | ||
[param: string]: any | ||
}; | ||
|
||
export interface CommonRequestOptions { | ||
$queryParameters ? : QueryParameters; | ||
$domain ? : string; | ||
$path ? : string | ((path: string) => string); | ||
$retries ? : number; // number of retries; see: https://github.com/visionmedia/superagent/blob/master/docs/index.md#retrying-requests | ||
$timeout ? : number; // request timeout in milliseconds; see: https://github.com/visionmedia/superagent/blob/master/docs/index.md#timeouts | ||
$deadline ? : number; // request deadline in milliseconds; see: https://github.com/visionmedia/superagent/blob/master/docs/index.md#timeouts | ||
} | ||
|
||
/** | ||
* | ||
* @class AddpropsApi | ||
* @param {(string)} [domainOrOptions] - The project domain. | ||
*/ | ||
export class AddpropsApi { | ||
|
||
private domain: string = \\"\\"; | ||
private errorHandlers: CallbackHandler[] = []; | ||
private requestHeadersHandler ? : RequestHeadersHandler; | ||
private configureAgentHandler ? : ConfigureAgentHandler; | ||
private configureRequestHandler ? : ConfigureRequestHandler; | ||
|
||
constructor(domain ? : string, private logger ? : Logger) { | ||
if (domain) { | ||
this.domain = domain; | ||
} | ||
} | ||
|
||
getDomain() { | ||
return this.domain; | ||
} | ||
|
||
addErrorHandler(handler: CallbackHandler) { | ||
this.errorHandlers.push(handler); | ||
} | ||
|
||
setRequestHeadersHandler(handler: RequestHeadersHandler) { | ||
this.requestHeadersHandler = handler; | ||
} | ||
|
||
setConfigureAgentHandler(handler: ConfigureAgentHandler) { | ||
this.configureAgentHandler = handler; | ||
} | ||
|
||
setConfigureRequestHandler(handler: ConfigureRequestHandler) { | ||
this.configureRequestHandler = handler; | ||
} | ||
|
||
private request(method: string, url: string, body: any, headers: RequestHeaders, queryParameters: QueryParameters, form: any, reject: CallbackHandler, resolve: CallbackHandler, opts: CommonRequestOptions) { | ||
if (this.logger) { | ||
this.logger.log(\`Call \${method} \${url}\`); | ||
} | ||
|
||
const agent = this.configureAgentHandler ? | ||
this.configureAgentHandler(request.default) : | ||
request.default; | ||
|
||
let req = agent(method, url); | ||
if (this.configureRequestHandler) { | ||
req = this.configureRequestHandler(req); | ||
} | ||
|
||
req = req.query(queryParameters); | ||
|
||
if (body) { | ||
req.send(body); | ||
|
||
if (typeof(body) === 'object' && !(body.constructor.name === 'Buffer')) { | ||
headers['Content-Type'] = 'application/json'; | ||
} | ||
} | ||
|
||
if (Object.keys(form).length > 0) { | ||
req.type('form'); | ||
req.send(form); | ||
} | ||
|
||
if (this.requestHeadersHandler) { | ||
headers = this.requestHeadersHandler({ | ||
...headers | ||
}); | ||
} | ||
|
||
req.set(headers); | ||
|
||
if (opts.$retries && opts.$retries > 0) { | ||
req.retry(opts.$retries); | ||
} | ||
|
||
if (opts.$timeout && opts.$timeout > 0 || opts.$deadline && opts.$deadline > 0) { | ||
req.timeout({ | ||
deadline: opts.$deadline, | ||
response: opts.$timeout | ||
}); | ||
} | ||
|
||
req.end((error, response) => { | ||
// an error will also be emitted for a 4xx and 5xx status code | ||
// the error object will then have error.status and error.response fields | ||
// see superagent error handling: https://github.com/visionmedia/superagent/blob/master/docs/index.md#error-handling | ||
if (error) { | ||
reject(error); | ||
this.errorHandlers.forEach(handler => handler(error)); | ||
} else { | ||
resolve(response); | ||
} | ||
}); | ||
} | ||
|
||
get_personURL(parameters: {} & CommonRequestOptions): string { | ||
let queryParameters: QueryParameters = {}; | ||
const domain = parameters.$domain ? parameters.$domain : this.domain; | ||
let path = '/persons'; | ||
if (parameters.$path) { | ||
path = (typeof(parameters.$path) === 'function') ? parameters.$path(path) : parameters.$path; | ||
} | ||
|
||
if (parameters.$queryParameters) { | ||
queryParameters = { | ||
...queryParameters, | ||
...parameters.$queryParameters | ||
}; | ||
} | ||
|
||
let keys = Object.keys(queryParameters); | ||
return domain + path + (keys.length > 0 ? '?' + (keys.map(key => key + '=' + encodeURIComponent(queryParameters[key])).join('&')) : ''); | ||
} | ||
|
||
/** | ||
* Gets \`Person\` object. | ||
* @method | ||
* @name AddpropsApi#get_person | ||
*/ | ||
get_person(parameters: {} & CommonRequestOptions): Promise < ResponseWithBody < 200, void >> { | ||
const domain = parameters.$domain ? parameters.$domain : this.domain; | ||
let path = '/persons'; | ||
if (parameters.$path) { | ||
path = (typeof(parameters.$path) === 'function') ? parameters.$path(path) : parameters.$path; | ||
} | ||
|
||
let body: any; | ||
let queryParameters: QueryParameters = {}; | ||
let headers: RequestHeaders = {}; | ||
let form: any = {}; | ||
return new Promise((resolve, reject) => { | ||
|
||
if (parameters.$queryParameters) { | ||
queryParameters = { | ||
...queryParameters, | ||
...parameters.$queryParameters | ||
}; | ||
} | ||
|
||
this.request('GET', domain + path, body, headers, queryParameters, form, reject, resolve, parameters); | ||
}); | ||
} | ||
|
||
} | ||
|
||
export default AddpropsApi;" | ||
`; | ||
|
||
exports[`Should resolve protected api 1`] = ` | ||
"// tslint:disable | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { "version": "0.0.1", "title": "your title" }, | ||
"paths": { | ||
"/persons": { | ||
"get": { | ||
"operationId": "get_person", | ||
"description": "Gets `Person` object.", | ||
"responses": { "200": { "description": "empty schema" } } | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"some_def": { "type": "object", "properties": { "some_def": { "type": "string" } }, "additionalProperties": false }, | ||
|
||
"test_add_props_01": { "type": "object", "properties": { "some_prop": { "type": "string" } } }, | ||
"test_add_props_02": { "type": "object", "properties": { "some_prop": { "type": "string" } }, "additionalProperties": false }, | ||
"test_add_props_03": { "type": "object", "properties": { "some_prop": { "type": "string" } }, "additionalProperties": true }, | ||
"test_add_props_04": { "type": "object", "properties": { "some_prop": { "type": "string" } }, "additionalProperties": { "type": "string" } }, | ||
"test_add_props_05": { "type": "object", "properties": { "some_prop": { "type": "string" } }, "additionalProperties": { "type": "object", "properties": { "nested_prop": { "type": "string" } } } }, | ||
"test_add_props_06": { "type": "object", "properties": { "some_prop": { "type": "string" } }, "additionalProperties": { "$ref": "#/definitions/some_def" } }, | ||
"test_add_props_07": { "type": "object" }, | ||
"test_add_props_08": { "type": "object", "additionalProperties": false }, | ||
"test_add_props_09": { "type": "object", "additionalProperties": true }, | ||
"test_add_props_10": { "type": "object", "additionalProperties": { "type": "string" } }, | ||
"test_add_props_11": { "type": "object", "additionalProperties": { "type": "object", "properties": { "nested_prop": { "type": "string" } } } }, | ||
"test_add_props_12": { "type": "object", "additionalProperties": { "$ref": "#/definitions/some_def" } } | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are the new test cases |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will be fixed/enhanced in a separate PR dealing with pre-defined
response
definitions...