Skip to content

Sugar response decorators for easier controller config #1672

@virkt25

Description

@virkt25

Description / Steps to reproduce / Feature proposal

Currently an OpenAPI response can only be set by providing the complete controller spec OR by passing it in the operation decorator. Describing a spec is very tedious and time consuming and we should provide an easier way to automagically assemble the spec.

Proposed Design

class MyController {
  @response.ok('todo model instance created')
  @post('/')
  myMethod(): Promise<Todo> {
    // implementation
  }
}

A few alternatives for the decorator:

// schema in this case can be a Model / TypeScript Type.
@response(statusCode: number, description: string, responseSchema: schema)
@response.ok(description: string, responseSchema: schema) // ok in this case would translate to a 200
@response.statusCode(description: string, responseSchema: schema) // statusCode being 200, 404, etc.
@response.ok(description: string) + @response.schema(Todo) // allows arbitrary schema registration with the OpenAPI spec.

Acceptance Criteria

  • Create a series of new "sugar" decorators to simplify OpenAPI response decorations for REST APIs. Proposed Decorators are as follows:
@response(200, 'description', 'application/custom-type', Customer)
@response(statusCode, Customer)
@response(statusCode, description, Customer)
@response(Customer) // Automagically generate statusCode, description (as done today), and assume json return type
// NOTE: Document that complex cases are dealt with via the operational decorators (@get, @post). 
  • The decorators should be "stackable", meaning the same decorator can be utilized multiple times on the same class method to specify multiple responses
class MyController {
@response(String)
@response(500, 'server error', Error)
hello() { // ... }
}
  • Update CLI controller templates to use the new decorator instead
  • Update example/todo, example/todo-list to use the new decorators
  • Tests
  • Docs
  • Blog

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions