Skip to content

OpenAPI decorator does not properly generate schemas of type Partial #1179

@shimks

Description

@shimks

Description / Steps to reproduce / Feature proposal

From todo.controllers.ts in example-getting-started:

    async createTodo(@requestBody() todo: Todo) {
    // TODO(bajtos) This should be handled by the framework
    // See https://github.com/strongloop/loopback-next/issues/118
      if (!todo.title) {
        return Promise.reject(new HttpErrors.BadRequest('title is required'));
      }
      return await this.todoRepo.create(todo);
    }

It should be noted that the spec generated by @requestBody is not correct since the argumnet that the operation takes in does not need an id property. One may think that Partial<Todo> may be the next best thing, but it's not possible at the moment to specify which properties are optional and which aren't.

We should find a way to generate the schema for a model when used as a parameter so that id property is not included in the generated schema

Proposed behavior:

@post('/')
makeStuff(@requestBody(Note) note: Partial<Note>) {}
  • Whenever requestBody is used on a model that extends Entity, always generate (or modify) its spec without the property that's been assigned as id.
  • pros:
    • simple ux
  • cons:
    • Not very flexible
    • requestBody becomes very overloaded

Acceptance Criteria:

@post('/exclude')
postWithout(@requestBody(Note, {excludeProperties: ['id']}) note: Partial<Note>) {}
@post('/include')
PostWith(@requestbody(Note, {includeProperties: ['title', 'description']) note: Partial<Note>) {}
  • explore more options from TypeScript (like Partial) to see if they can infer or provide us with additional data/metadata

  • allow requestBody (or other new decorator to create the appropriate metadata) to take in the model type and schema generation options to produce a custom schema

    • specifying excludeProperties should generate an OpenAPI Schema that lacks the given keys and their values when converting from the JSON Schema generated in rest
    • specifying includeProperties should generate an OpenAPI Schema that contains only the given keys and their values when converted from the JSON Schema generated in rest
  • Backwards compatibility must be preserved (@requestBody(spec))

  • related to fix(repository): fix broken code in readme #1121

See Reporting Issues for more tips on writing good issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    JugglerOpenAPIRESTIssues related to @loopback/rest package and REST transport in generalSchemafeature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions