Skip to content

Commit

Permalink
Merge pull request #19 from crazyfactory/18-fix-204-response
Browse files Browse the repository at this point in the history
18 fix 204 response
  • Loading branch information
wmathes authored Jul 10, 2017
2 parents bbe7792 + cb2c5c9 commit b7bf826
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
51 changes: 47 additions & 4 deletions src/GeneratorHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ describe("GeneratorHelpers", () => {
"summary": "",
"description": "Zone details response payload",
"responses": {
"200": {
"201": {
"description": "Zone details response payload",
"schema": {
"$ref":"#/definitions/zones_detail"
Expand Down Expand Up @@ -857,8 +857,12 @@ describe("GeneratorHelpers", () => {
expect(methods[0].classNames).to.deep.equal(["auth"]);
});
it("returns correct method's returnType", () => {
expect(methods[0].returnType).to.equal("authentication");
expect(methods[11].returnType).to.equal(null);
// 200 response
expect(methods[7].returnType).to.equal("zones_list");
// 201 response
expect(methods[8].returnType).to.equal("zones_detail");
// 204 response
expect(methods[11].returnType).to.equal("void");
});
it("returns correct method's httpMethod", () => {
expect(methods[8].httpMethod).to.equal(HttpMethod.POST);
Expand Down Expand Up @@ -915,6 +919,45 @@ describe("GeneratorHelpers", () => {
required: true
}]);
});
it("throws missing response", () => {
const localPaths: IPathsData = {
"/products": {
"get": {
"tags": [
"products"
],
"operationId": "products-list",
"produces": [
"application/json"
],
"consumes": [
"application/json"
],
"summary": "",
"description": "Products list response payload",
"responses": {
"500": {
"description": "Error response",
"schema": {
"$ref":"#/definitions/error_response"
}
}
},
"parameters": [],
"security": [
{
"bearer": [
"user"
]
}
]
}
}
};
expect(GeneratorHelpers.getApiMethods.bind(GeneratorHelpers, localPaths)).to.throw(
"This api method products-list does not define response for success status"
);
});
it("throws unknown param", () => {
const localPaths: IPathsData = {
"/auth": {
Expand Down Expand Up @@ -975,7 +1018,7 @@ describe("GeneratorHelpers", () => {
}
};
expect(GeneratorHelpers.getApiMethods.bind(GeneratorHelpers, localPaths)).to.throw("Unknown param type -> ABC");
})
});
it("returns correct method's url", () => {
expect(methods[11].url).to.equal("/zones/{id}");
});
Expand Down
20 changes: 17 additions & 3 deletions src/GeneratorHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,23 @@ export class GeneratorHelpers {
method.classNames = paths[url][httpMethod].tags;

const responses = paths[url][httpMethod].responses;
if (responses["200"]) {
const returnTypeRef = responses["200"]["schema"]["$ref"];
method.returnType = returnTypeRef.substr(returnTypeRef.lastIndexOf("/") + 1);
const successCodes = ["200", "201", "202", "203", "204", "205", "206", "207", "208", "226"];

for (const successCode of successCodes) {
if (responses[successCode]) {
if (successCode === "204") {
method.returnType = "void";
}
else {
const returnTypeRef = responses[successCode]["schema"]["$ref"];
method.returnType = returnTypeRef.substr(returnTypeRef.lastIndexOf("/") + 1);
}
break;
}
}

if (!method.returnType) {
throw new Error(`This api method ${method.name} does not define response for success status`);
}

method.httpMethod = this.cleanHttpMethod(httpMethod);
Expand Down
12 changes: 3 additions & 9 deletions src/TypeScriptGenerator/TsControllerGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("TsControllerGenerator", () => {
const zonesDeleteMethod: IApiMethod = {
name: "zones-delete",
classNames: ["zones"],
returnType: null,
returnType: "void",
httpMethod: HttpMethod.DELETE,
allParams: [
{
Expand Down Expand Up @@ -119,11 +119,11 @@ describe("TsControllerGenerator", () => {
const categoriesController: IApiController = {
name: "categories",
methods: [categoriesListMethod, categoriesDetailMethod]
}
};
const zonesController: IApiController = {
name: "zones",
methods: [zonesUpdateMethod, zonesDeleteMethod]
}
};

describe("generateApiControllerNodes()", () => {
let codes: ICode[];
Expand Down Expand Up @@ -216,12 +216,6 @@ describe("TsControllerGenerator", () => {
const code: ICode = gen.generateApiMethodCode(categoriesListMethod);
expect(code.toString()).to.contains("public categoriesList(options?: IFetchRequest):");
});

it("sets return type to void if returnType is null", () => {
const gen = new TsControllerGenerator([]);
const code: ICode = gen.generateApiMethodCode(zonesDeleteMethod);
expect(code.toString()).to.contains("options?: IFetchRequest): void {");
});
});

describe("getParamsDefinition()", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/TypeScriptGenerator/TsControllerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class TsControllerGenerator implements IGenerator {
paramsAndOptions = `options?: IFetchRequest`;
}

const returnType = apiMethod.returnType ? "Promise<"+ this.prefix + apiMethod.returnType.toPascalCase() + ">" : "void";
const returnType = `Promise<${this.prefix + apiMethod.returnType.toPascalCase()}>`;
let parent: ICode = new Code(`public ${apiMethod.name.toCamelCase()}(${paramsAndOptions}): ${returnType}`);
let fetchRequestString: string = `return this.client.process({...${this.getFetchRequestString(apiMethod)}, ...options} as IFetchRequest);`;
let child: ICode = new Code(fetchRequestString);
Expand Down

0 comments on commit b7bf826

Please sign in to comment.