Skip to content

Commit

Permalink
Bd/fix path param ordering (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
bavardage authored and bulldozer-bot[bot] committed Jul 26, 2019
1 parent a876084 commit bae8d46
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
5 changes: 5 additions & 0 deletions changelog/@unreleased/path-param-ordering.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Fix a problem with the generated code in the case where path params show up in different orders in the path template and the args block.
links:
- https://github.com/palantir/conjure-typescript/pull/100
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { IHttpApiBridge, MediaType } from "conjure-client";

export interface IOutOfOrderPathService {
foo(param1: string, param2: string): Promise<void>;
}

export class OutOfOrderPathService {
constructor(private bridge: IHttpApiBridge) {
}

public foo(param1: string, param2: string): Promise<void> {
return this.bridge.callEndpoint<void>({
data: undefined,
endpointName: "foo",
endpointPath: "/{param2}/{param1}",
headers: {
},
method: "GET",
pathArguments: [
param2,

param1,
],
queryArguments: {
},
requestMediaType: MediaType.APPLICATION_JSON,
responseMediaType: MediaType.APPLICATION_JSON,
});
}
}
39 changes: 39 additions & 0 deletions src/commands/generate/__tests__/serviceGeneratorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,45 @@ describe("serviceGenerator", () => {
assertOutputAndExpectedAreEqual(outDir, expectedDir, "services/paramTypeService.ts");
});

it("handles out of order path params", async () => {
await generateService(
{
endpoints: [
{
args: [
{
argName: "param1",
markers: [],
paramType: {
path: {},
type: "path",
},
type: stringType,
},
{
argName: "param2",
markers: [],
paramType: {
path: {},
type: "path",
},
type: stringType,
},
],
endpointName: "foo",
httpMethod: HttpMethod.GET,
httpPath: "/{param2}/{param1}",
markers: [],
},
],
serviceName: { name: "OutOfOrderPathService", package: "com.palantir.services" },
},
new Map(),
simpleAst,
);
assertOutputAndExpectedAreEqual(outDir, expectedDir, "services/outOfOrderPathService.ts");
});

it("handles header auth-type", async () => {
await generateService(
{
Expand Down
14 changes: 13 additions & 1 deletion src/commands/generate/serviceGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ function generateEndpointBody(
}
});

const pathParamsFromPath = parsePathParamsFromPath(endpointDefinition.httpPath);

if (bodyArgs.length > 1) {
throw Error("endpoint cannot have more than one body arg, found: " + bodyArgs.length);
}
Expand Down Expand Up @@ -201,7 +203,7 @@ function generateEndpointBody(
writer.writeLine(`method: "${endpointDefinition.httpMethod}",`);

writer.write("pathArguments: [");
pathArgNames.forEach(pathArgName => writer.indent().writeLine(pathArgName + ","));
pathParamsFromPath.forEach(pathArgName => writer.indent().writeLine(pathArgName + ","));
writer.writeLine("],");

writer.write("queryArguments: {");
Expand All @@ -222,3 +224,13 @@ function mediaType(conjureType?: IType) {
}
return "MediaType.APPLICATION_JSON";
}

function parsePathParamsFromPath(httpPath: string): string[] {
// first fix up the path to remove any ':.+' stuff in path params
const fixedPath = httpPath.replace(/{(.*):[^}]*}/, "{$1}");
// follow-up by just pulling out any path segment with a starting '{' and trailing '}'
return fixedPath
.split("/")
.filter(segment => segment.startsWith("{") && segment.endsWith("}"))
.map(segment => segment.slice(1, -1));
}

0 comments on commit bae8d46

Please sign in to comment.