Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit schema excluding certain model properties #2653

Closed
9 tasks done
bajtos opened this issue Mar 28, 2019 · 3 comments
Closed
9 tasks done

Emit schema excluding certain model properties #2653

bajtos opened this issue Mar 28, 2019 · 3 comments

Comments

@bajtos
Copy link
Member

bajtos commented Mar 28, 2019

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

@bajtos
Copy link
Member Author

bajtos commented May 21, 2019

Pick<TodoList, Exclude<keyof TodoList, 'id'>>

TypeScript 3.5 is going to introduce a new type Omit (see the announcement post), let's leverage it here.

- Pick<TodoList, Exclude<keyof TodoList, 'id'>>`
+ Omit<TodoList, 'id'>

@bajtos
Copy link
Member Author

bajtos commented Jul 12, 2019

@nabdelgadir What is the remaining work now that #3309 was landed? Could you please update checkboxes in the acceptance criteria to match the new status and perhaps add a new task for adding support for PK with a custom name (not id).

@nabdelgadir
Copy link
Contributor

nabdelgadir commented Jul 12, 2019

@bajtos I'm assuming you were referring to #3297, but after #3309 is landed we can close this issue.

perhaps add a new task for adding support for PK with a custom name (not id).

I'm working on custom names but I'll create the new issue so we can close this one after #3309 is landed.

New issue: #3344.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants