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

Bd/fix path param ordering #100

Merged
merged 5 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
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,

bavardage marked this conversation as resolved.
Show resolved Hide resolved
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
19 changes: 18 additions & 1 deletion src/commands/generate/serviceGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ function generateEndpointBody(
}
});

const pathParamsFromPath = parsePathParamsFromPath(endpointDefinition.httpPath);
pathParamsFromPath.forEach(pathParam => {
bavardage marked this conversation as resolved.
Show resolved Hide resolved
if (pathArgNames.indexOf(pathParam) === -1) {
throw new Error("path parameter present in path template but not provided as endpoint param: " + pathParam);
}
});

if (bodyArgs.length > 1) {
throw Error("endpoint cannot have more than one body arg, found: " + bodyArgs.length);
}
Expand Down Expand Up @@ -201,7 +208,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 +229,13 @@ function mediaType(conjureType?: IType) {
}
return "MediaType.APPLICATION_JSON";
}

function parsePathParamsFromPath(httpPath: string): string[] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so I could replace this method with

[... httpPath.matchAll(/\{([^:\}]+)([:]*[^\}]*)\}/g)].map(x => x[1])

but not convinced that's easier to read

// 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));
}