Skip to content

Commit

Permalink
fix(openapi): type "id" field according to ZModel schema type (#1929)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Dec 30, 2024
1 parent 93246f3 commit f609c86
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/plugins/openapi/src/rest-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,16 +906,24 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
},
};

let idFieldSchema: OAPI.SchemaObject = { type: 'string' };
if (idFields.length === 1) {
// FIXME: JSON:API actually requires id field to be a string,
// but currently the RESTAPIHandler returns the original data
// type as declared in the ZModel schema.
idFieldSchema = this.fieldTypeToOpenAPISchema(idFields[0].type);
}

if (mode === 'create') {
// 'id' is required if there's no default value
const idFields = model.fields.filter((f) => isIdField(f));
if (idFields.length === 1 && !hasAttribute(idFields[0], '@default')) {
properties = { id: { type: 'string' }, ...properties };
properties = { id: idFieldSchema, ...properties };
toplevelRequired.unshift('id');
}
} else {
// 'id' always required for read and update
properties = { id: { type: 'string' }, ...properties };
properties = { id: idFieldSchema, ...properties };
toplevelRequired.unshift('id');
}

Expand Down
26 changes: 24 additions & 2 deletions packages/plugins/openapi/tests/openapi-restful.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ model Bar {

const { name: output } = tmp.fileSync({ postfix: '.yaml' });

const options = buildOptions(model, modelFile, output, '3.1.0');
const options = buildOptions(model, modelFile, output, specVersion);
await generate(model, options, dmmf);

console.log(`OpenAPI specification generated for ${specVersion}: ${output}`);
Expand Down Expand Up @@ -324,7 +324,7 @@ model Foo {

const { name: output } = tmp.fileSync({ postfix: '.yaml' });

const options = buildOptions(model, modelFile, output, '3.1.0');
const options = buildOptions(model, modelFile, output, specVersion);
await generate(model, options, dmmf);

console.log(`OpenAPI specification generated for ${specVersion}: ${output}`);
Expand All @@ -340,6 +340,28 @@ model Foo {
}
});

it('int field as id', async () => {
const { model, dmmf, modelFile } = await loadZModelAndDmmf(`
plugin openapi {
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
}
model Foo {
id Int @id @default(autoincrement())
}
`);

const { name: output } = tmp.fileSync({ postfix: '.yaml' });

const options = buildOptions(model, modelFile, output, '3.0.0');
await generate(model, options, dmmf);
console.log(`OpenAPI specification generated: ${output}`);
await OpenAPIParser.validate(output);

const parsed = YAML.parse(fs.readFileSync(output, 'utf-8'));
expect(parsed.components.schemas.Foo.properties.id.type).toBe('integer');
});

it('exposes individual fields from a compound id as attributes', async () => {
const { model, dmmf, modelFile } = await loadZModelAndDmmf(`
plugin openapi {
Expand Down

0 comments on commit f609c86

Please sign in to comment.