Skip to content

Commit

Permalink
fix(#153): only convert hyphenated path parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
janwvjaarsveld authored and astahmer committed Jun 6, 2023
1 parent a5dc1c4 commit 2486ce5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-toys-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-zod-client": patch
---

Fix issue where path parameters containing underscores were also converted to camelCase
4 changes: 2 additions & 2 deletions lib/src/getZodiosEndpointDefinitionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type { TemplateContext } from "./template-context";
import {
asComponentSchema,
pathParamToVariableName,
replaceHyphatedPath,
replaceHyphenatedPath,
normalizeString,
pathToVariableName,
} from "./utils";
Expand Down Expand Up @@ -159,7 +159,7 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
const operationName = getOperationAlias(path, method, operation);
const endpointDefinition: EndpointDefinitionWithRefs = {
method: method as EndpointDefinitionWithRefs["method"],
path: replaceHyphatedPath(path),
path: replaceHyphenatedPath(path),
alias: operationName,
description: operation.description,
requestFormat: "json",
Expand Down
9 changes: 7 additions & 2 deletions lib/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ const prefixStringStartingWithNumberIfNeeded = (str: string) => {
const pathParamWithBracketsRegex = /({\w+})/g;
const wordPrecededByNonWordCharacter = /[^\w\-]+/g;

export const pathParamToVariableName = (name: string) => snakeToCamel(name.replaceAll("-", "_"));
export const pathParamToVariableName = (name: string) => {
// Replace all underscores with # to preserve them when doing snakeToCamel
const preserveUnderscore = name.replaceAll("_", "#");
return snakeToCamel(preserveUnderscore.replaceAll("-", "_")).replaceAll("#", "_");
};

const matcherRegex = /{(\b\w+(?:-\w+)*\b)}/g;
export const replaceHyphatedPath = (path: string) => {
export const replaceHyphenatedPath = (path: string) => {
const matches = path.match(matcherRegex);
if (matches === null) {
return path.replaceAll(matcherRegex, ":$1");
}

matches.forEach((match) => {
const replacement = pathParamToVariableName(match.replaceAll(matcherRegex, ":$1"));
path = path.replaceAll(match, replacement);
Expand Down
48 changes: 48 additions & 0 deletions lib/tests/hyphenated-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ test("common-parameters", async () => {
},
},
},
"/pet/{owner_name}": {
post: {
parameters: [{ name: "owner_name", in: "path", required: true, schema: { type: "string" } }],
responses: {
"200": {
description: "Successful operation",
content: { "application/json": { schema: { type: "boolean" } } },
},
},
},
},
"/pet/{owner_name-id}": {
post: {
parameters: [{ name: "owner_name-id", in: "path", required: true, schema: { type: "string" } }],
responses: {
"200": {
description: "Successful operation",
content: { "application/json": { schema: { type: "boolean" } } },
},
},
},
},
},
};

Expand All @@ -28,6 +50,32 @@ test("common-parameters", async () => {
import { z } from "zod";
const endpoints = makeApi([
{
method: "post",
path: "/pet/:owner_name",
requestFormat: "json",
parameters: [
{
name: "owner_name",
type: "Path",
schema: z.string(),
},
],
response: z.boolean(),
},
{
method: "post",
path: "/pet/:owner_nameId",
requestFormat: "json",
parameters: [
{
name: "owner_nameId",
type: "Path",
schema: z.string(),
},
],
response: z.boolean(),
},
{
method: "post",
path: "/pet/:petId/uploadImage",
Expand Down

1 comment on commit 2486ce5

@vercel
Copy link

@vercel vercel bot commented on 2486ce5 Jun 6, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.