Skip to content

Commit 7741f07

Browse files
authored
fix(hono): preserve parameter name as defined in the OAS (#1921)
* fix: preserve parameter name as defined in the OAS. * style: remove unnecessary parenthesis * fix(hono): fix param types * chore(hono): update hono-with-zod sample * chore(hono): update hono-with-fetch-client sample * chore(hono): remove /cats/cat_id endpoint from yaml * chore(hono): remove cat definition * chore(hono): remove cat handler
1 parent cba57d1 commit 7741f07

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

packages/hono/src/index.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import {
1818
NormalizedOutputOptions,
1919
pascal,
2020
upath,
21+
getParamsInPath,
22+
sanitize,
23+
camel,
2124
} from '@orval/core';
2225
import { generateZod } from '@orval/zod';
2326
import fs from 'fs-extra';
@@ -542,11 +545,22 @@ const factory = createFactory();`;
542545
};
543546

544547
const getContext = (verbOption: GeneratorVerbOptions) => {
545-
const paramType = verbOption.params.length
546-
? `param: {\n ${verbOption.params
547-
.map((property) => property.definition)
548-
.join(',\n ')},\n },`
549-
: '';
548+
let paramType = '';
549+
if (verbOption.params.length) {
550+
const params = getParamsInPath(verbOption.pathRoute).map((name) => {
551+
const param = verbOption.params.find(
552+
(p) => p.name === sanitize(camel(name), { es5keyword: true }),
553+
);
554+
const definition = param?.definition.split(':')[1];
555+
const required = param?.required ?? false;
556+
return {
557+
definition: `${name}${!required ? '?' : ''}:${definition}`,
558+
};
559+
});
560+
paramType = `param: {\n ${params
561+
.map((property) => property.definition)
562+
.join(',\n ')},\n },`;
563+
}
550564

551565
const queryType = verbOption.queryParams
552566
? `query: ${verbOption.queryParams?.schema.name},`

packages/hono/src/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { camel, sanitize } from '@orval/core';
1+
import { sanitize } from '@orval/core';
22

33
const hasParam = (path: string): boolean => /[^{]*{[\w*_-]*}.*/.test(path);
44

@@ -7,7 +7,7 @@ const getRoutePath = (path: string): string => {
77
if (!matches?.length) return path; // impossible due to regexp grouping here, but for TS
88

99
const prev = matches[1];
10-
const param = sanitize(camel(matches[2]), {
10+
const param = sanitize(matches[2], {
1111
es5keyword: true,
1212
underscore: true,
1313
dash: true,

0 commit comments

Comments
 (0)