Skip to content

Emit schema excluding certain model properties #2653

Closed
@bajtos

Description

@bajtos

Allow Controller methods implementing CREATE operation to describe their input data as "a model without id and _rev properties ". This story requires #2629 and #2631 to be implemented first.

To emit schema with certain properties excluded, we should add a new schema-generation option called exclude & accepting a list of property names to remove from the schema. To allow TypeScript compiler to verify that exclude items are valid property names, we should leverage TypeScript generics and keyof keyword.

export interface JsonSchemaOptions<T extends object = any>{
  /// List of properties to exclude from the schema
  exclude?: (keyof T)[];
}

An example showing a controller method excluding the property id:

class TodoListController {
  // ...

  @post('/todo-lists', {
    responses: {
      // left out for brevity
    },
  })
  async create(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(TodoList, {exclude: ['id']}),
          /***** ^^^ THIS IS IMPORTANT - OPENAPI SCHEMA ^^^ ****/
        },
      },
    })
    obj: Omit<TodoList, 'id'>,
    /***** ^^^ THIS IS IMPORTANT - TYPESCRIPT TYPE ^^^ ****/
  ): Promise<TodoList> {
    return await this.todoListRepository.create(obj);
  }
}

See 887ae84 for a spike showing proposed implementation details.

Acceptance criteria

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions