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/enhance support for "additionalProperties" #86

Merged
merged 1 commit into from
Jul 17, 2019
Merged
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
280 changes: 277 additions & 3 deletions __tests__/__snapshots__/runner.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ export type Order = {
'shipDate' ? : string;
'status' ? : \\"placed\\" | \\"approved\\" | \\"delivered\\";
'complete' ? : boolean;
} & {
[key: string]: any;
};

export type User = {
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 >> {
Copy link
Author

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...

const domain = parameters.$domain ? parameters.$domain : this.domain;
let path = '/store/inventory';
if (parameters.$path) {
Expand Down Expand Up @@ -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;
};
Copy link
Author

Choose a reason for hiding this comment

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

the types test_add_props_* have been validated according to the description about how to handle the different cases for additionalProperties ...


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

Expand Down
29 changes: 29 additions & 0 deletions __tests__/fixtures/addProps/swagger.json
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" } }
}
}
Copy link
Author

Choose a reason for hiding this comment

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

these are the new test cases

4 changes: 4 additions & 0 deletions __tests__/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ var testCases = [
desc: "Should resolve references",
fixture: "ref"
},
{
desc: "Should resolve \"additionalProperties\"",
fixture: "addProps"
},
{
desc: "Real world: Uber",
fixture: "uber"
Expand Down
1 change: 1 addition & 0 deletions src/swagger/Swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface SwaggerType {
readonly properties: {
readonly [index: string]: SwaggerType;
};
readonly additionalProperties?: boolean | SwaggerType;
}

export interface SwaggerArray extends SwaggerType {
Expand Down
5 changes: 3 additions & 2 deletions src/test-helpers/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ export function makeEmptyTypeSpec(): TypeSpec {
description: undefined,
isEnum: false,
isArray: false,
isDictionary: false,
isObject: false,
isRef: false,
isNullable: false,
isRequired: true,
tsType: undefined,
isAtomic: false,
target: undefined,
properties: undefined
properties: undefined,
hasAdditionalProperties: false,
additionalPropertiesType: undefined
};
}
Loading