Skip to content

Commit

Permalink
fix(dispatcher): Fix dispatcher.findOperation to use case insensitive…
Browse files Browse the repository at this point in the history
… match
  • Loading branch information
dethell committed Apr 9, 2024
1 parent 950ba77 commit f2d4c25
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/server/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,28 @@ export class Dispatcher {
return types;
}

private findOperation(
path: string,
method: HttpMethods,
): OpenApiOperation | undefined {
for (const key in this.openApiDocument?.paths) {
if (key.toLowerCase() === path.toLowerCase()) {
return this.openApiDocument.paths[key]?.[
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
method.toLowerCase() as Lowercase<HttpMethods>
];
break;
}
}

return undefined;
}

public operationForPathAndMethod(
path: string,
method: HttpMethods,
): OpenApiOperation | undefined {
const operation: OpenApiOperation | undefined =
this.openApiDocument?.paths[path]?.[
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
method.toLowerCase() as Lowercase<HttpMethods>
];
const operation = this.findOperation(path, method);

if (operation === undefined) {
return undefined;
Expand Down Expand Up @@ -207,13 +220,15 @@ export class Dispatcher {
// eslint-disable-next-line sonarjs/cognitive-complexity, max-statements
public async request({
body,
config,
headers = {},
method,
path,
query,
req,
}: {
body: unknown;
config?: Config;
headers: {
[key: string]: string;
};
Expand All @@ -238,7 +253,11 @@ export class Dispatcher {
}

const { matchedPath } = this.registry.handler(path);
const operation = this.operationForPathAndMethod(matchedPath, method);
const operation = this.operationForPathAndMethod(
matchedPath,
method,
config?.ignorePathCase,
);

const response = await this.registry.endpoint(
method,
Expand Down
48 changes: 48 additions & 0 deletions test/server/dispatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,51 @@ describe("given a request that contains the OpenApi basePath", () => {
expect(response.body).toBe("ok");
});
});

describe("given a request that contains the differently cased path", () => {
it("correctly returns the desired path response even when case of path does not match", async () => {
const registry = new Registry();

registry.add("/abc", {
POST() {
return {
body: "ok",
status: 200,
};
},
});

const dispatcher = new Dispatcher(registry, new ContextRegistry(), {
paths: {
"/Abc": {
post: {
responses: {
200: {
content: {
"text/plain": {
schema: {
type: "string",
},
},
},
},
},
},
},
},
});

const response = await dispatcher.request({
body: "",
headers: {},
method: "POST",
path: "/abc",
query: {},
req: { path: "/abc" },
});

expect(response.status).toBe(200);

expect(response.body).toBe("ok");
});
});

0 comments on commit f2d4c25

Please sign in to comment.