loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

Syntactic sugar for `requestBody()` with custom schema options #3257

Closed bajtos closed 3 years ago

bajtos commented 5 years ago

Suggestion

At the moment, it's cumbersome to describe a request-body parameter with a schema built from a model with custom options.

See e.g. https://github.com/strongloop/loopback-next/pull/3199

class NoteController {
  // ...

  @patch('/notes/{id}', {
    responses: {
      '204': {
        description: 'Note PATCH success',
      },
    },
  })
  async updateById(
    @param.path.number('id') id: number,

    /*** LOOK AT THIS LONG BLOG OF CODE: ***/
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Note, {partial: true}),
        },
      },
    })
    /*** END ***/
    note: Partial<Note>,
  ): Promise<void> {
    await this.noteRepository.updateById(id, note);
  }
}

Let's find a more concise way how to describe such request bodies.

Examples

Few of many possible solutions:

1) New decorator

class NoteController {
  // ...

  @patch('/notes/{id}', {
    responses: {
      '204': {
        description: 'Note PATCH success',
      },
    },
  })
  async updateById(
    @param.path.number('id') id: number,

    @requestBodyWithSchema(getModelSchemaRef(Note, {partial: true}))
    note: Partial<Note>,
  ): Promise<void> {
    await this.noteRepository.updateById(id, note);
  }
}

2) New helper

class NoteController {
  // ...

  @patch('/notes/{id}', {
    responses: {
      '204': {
        description: 'Note PATCH success',
      },
    },
  })
  async updateById(
    @param.path.number('id') id: number,

    @requestBody(bodyFromSchema(getModelSchemaRef(Note, {partial: true})))
    note: Partial<Note>,
  ): Promise<void> {
    await this.noteRepository.updateById(id, note);
  }
}

Important considerations:

The example snippets above meets both criteria.

Acceptance criteria

bajtos commented 4 years ago

Related:

stale[bot] commented 3 years ago

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] commented 3 years ago

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.