Skip to content

Commit

Permalink
OpenApi @@id support (#1757)
Browse files Browse the repository at this point in the history
Co-authored-by: Yiming <yiming@whimslab.io>
  • Loading branch information
thomassnielsen and ymc9 authored Oct 8, 2024
1 parent 2a8f4df commit a2c78a1
Show file tree
Hide file tree
Showing 4 changed files with 1,752 additions and 452 deletions.
13 changes: 9 additions & 4 deletions packages/plugins/openapi/src/rest-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,17 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
private generateFilterParameters(model: DataModel) {
const result: OAPI.ParameterObject[] = [];

const hasMultipleIds = model.fields.filter((f) => isIdField(f)).length > 1;

for (const field of model.fields) {
if (isForeignKeyField(field)) {
// no filtering with foreign keys because one can filter
// directly on the relationship
continue;
}

if (isIdField(field)) {
// For multiple ids, make each id field filterable like a regular field
if (isIdField(field) && !hasMultipleIds) {
// id filter
result.push(this.makeFilterParameter(field, 'id', 'Id filter'));
continue;
Expand Down Expand Up @@ -843,7 +846,9 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {
}

private generateModelEntity(model: DataModel, mode: 'read' | 'create' | 'update'): OAPI.SchemaObject {
const fields = model.fields.filter((f) => !isIdField(f));
const idFields = model.fields.filter((f) => isIdField(f));
// For compound ids, each component is also exposed as a separate field
const fields = idFields.length > 1 ? model.fields : model.fields.filter((f) => !isIdField(f));

const attributes: Record<string, OAPI.SchemaObject> = {};
const relationships: Record<string, OAPI.ReferenceObject | OAPI.SchemaObject> = {};
Expand Down Expand Up @@ -886,8 +891,8 @@ export class RESTfulOpenAPIGenerator extends OpenAPIGeneratorBase {

if (mode === 'create') {
// 'id' is required if there's no default value
const idField = model.fields.find((f) => isIdField(f));
if (idField && !hasAttribute(idField, '@default')) {
const idFields = model.fields.filter((f) => isIdField(f));
if (idFields.length && idFields.every((f) => !hasAttribute(f, '@default'))) {
properties = { id: { type: 'string' }, ...properties };
toplevelRequired.unshift('id');
}
Expand Down
Loading

0 comments on commit a2c78a1

Please sign in to comment.