Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
50 changes: 41 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"postbuild": "chmod u+x dist/cli.js"
},
"dependencies": {
"openapi-typescript": "^7.9.1"
"openapi-typescript": "https://github.com/luqasn/openapi-typescript/releases/download/v7.11.1-preview/openapi-typescript-7.11.1-preview.tgz"
},
"peerDependencies": {
"typescript": "^5.9.3"
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/typegen/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ function write(dirName: string, fileName: string, data: string) {
async function createSpecTypes(specPath: string, outputDir: string) {
const absolutePath = path.resolve(specPath);
const url = pathToFileURL(absolutePath)
const ast = await openapiTS(url);

const ast = await openapiTS(url, {
makeParametersWithDefaultNotUndefined: true
});
const ts = astToString(ast);

return write(outputDir, 'spec.ts', ts);
Expand Down
22 changes: 20 additions & 2 deletions packages/lib/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,34 @@ export class OpenApi<T> {
return api;
}

protected injectDefaults(parsed: Params, parameterMap: Record<string, OpenAPIV3_1.ParameterBaseObject>): Params {
for (const [name, p] of Object.entries(parameterMap)) {
if (p.schema === undefined) {
continue
}
if ("$ref" in p.schema) {
throw new Error('this should never happen, schema should be dereferenced by openapi-backend')
}
if (p.schema.default !== undefined && parsed[name] === undefined) {
parsed[name] = p.schema.default
}
}
return parsed;
}

protected parseParams(rawParams: StringParams,
operation: OpenAPI.Operation,
type: ParameterType,
errors: Ajv.ErrorObject[]): Params {
const parameterMap = getParameterMap(operation, type)
// This is mostly used to coerce types, which openapi-backend does internally but then throws away
return matchSchema<StringParams, Params>(
const parsed = matchSchema<StringParams, Params>(
this.paramValidator,
rawParams,
getParametersSchema(getParameterMap(operation, type)),
getParametersSchema(parameterMap),
errors);

return this.injectDefaults(parsed, parameterMap)
}

protected parseRequest(apiContext: OpenAPI.Context): Request {
Expand Down
1 change: 1 addition & 0 deletions packages/test/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ components:
schemas:
Title:
type: string
default: Mrs
enum:
- Mr
- Mrs
Expand Down
15 changes: 14 additions & 1 deletion packages/test/src/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function getTypeMap(obj: any) {

const operations: OperationHandlers<unknown> = {
greet: req => {
const {params: {name}, query: {title = ''}} = req;
const {params: {name}, query: {title}} = req;

return {
message: greet(title, name),
Expand Down Expand Up @@ -61,6 +61,19 @@ describe('API tests', () => {
}
});
expect(res.statusCode).toEqual(200);
expect(res.body).toEqual({message: 'Hello, Mr John Doe'})
});

it('Should set default values for parameters automatically', async () => {
const res = await api.handleRequest({
method: 'GET',
path: '/greet/Jane%20Doe',
headers: {
authorization: 'true',
}
});
expect(res.statusCode).toEqual(200);
expect(res.body).toEqual({message: 'Hello, Mrs Jane Doe'})
});

it('Should handle a valid request with implicit status 201', async () => {
Expand Down