-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simplify request body decorator
- Loading branch information
Showing
8 changed files
with
420 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
## Improve the UX of @requestBody() | ||
|
||
The original discussion is tracked in issue [Spike: simplify requestBody annotation with schema options](https://github.com/strongloop/loopback-next/issues/2654). | ||
|
||
The current @requestBody() can only | ||
|
||
- takes in an entire request body specification with very nested media type objects | ||
or | ||
- generate the schema inferred from the parameter type | ||
|
||
To simplify the signature, this spike PR introduces a 2nd parameter `schemaOptions` to configure the schema. The new decorator `newRequestBody1` is written in file 'request-body.options1.decorator.ts' | ||
|
||
### Create - exclude properties | ||
|
||
Take "Create a new product with excluded properties" as an example: | ||
|
||
```ts | ||
// Provide the description as before | ||
const requestBodySpec = { | ||
description: 'Create a product', | ||
required: true, | ||
}; | ||
|
||
// Provide the options that configure your schema | ||
const excludeOptions = { | ||
// Using advanced ts types like `Exclude<>`, `Partial<>` results in | ||
// `MetadataInspector.getDesignTypeForMethod(target, member)` only | ||
// returns `Object` as the param type, which loses the model type's info. | ||
// Therefore user must provide the model type in options. | ||
[TS_TYPE_KEY]: Product, | ||
// Make sure you give the custom schema a unique schema name, | ||
// this name will be used as the reference name | ||
// like `$ref: '#components/schemas/ProductWithoutID'` | ||
schemaName: 'ProductWithoutID', | ||
// The excluded properties | ||
exclude: ['id'] | ||
} | ||
|
||
// The decorator takes in the option without having a nested content object | ||
class MyController1 { | ||
@post('/Product') | ||
create(@newRequestBody1( | ||
requestBodySpec, | ||
excludeOptions | ||
) product: Exclude<Product, ['id']>) { } | ||
} | ||
``` | ||
|
||
### Update - partial properties | ||
|
||
```ts | ||
const requestSpecForUpdate = { | ||
description: 'Update a product', | ||
required: true, | ||
}; | ||
|
||
const partialOptions = { | ||
[TS_TYPE_KEY]: Product, | ||
schemaName: 'PartialProduct', | ||
partial: true | ||
} | ||
|
||
class MyController2 { | ||
@put('/Product') | ||
update(@newRequestBody1( | ||
requestSpecForUpdate, | ||
partialOptions | ||
) product: Partial<Product>) { } | ||
} | ||
``` | ||
|
161 changes: 161 additions & 0 deletions
161
...penapi-v3/src/__tests__/unit/decorators/request-body/spike.request-body.decorator.unit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/openapi-v3 | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import { belongsTo, Entity, hasMany, model, property } from '@loopback/repository'; | ||
import { expect } from '@loopback/testlab'; | ||
import { getControllerSpec, post, put } from '../../../..'; | ||
import { TS_TYPE_KEY } from '../../../../controller-spec'; | ||
import { newRequestBody1 } from '../../../../decorators/request-body.option1.decorator'; | ||
|
||
describe.only('spike - requestBody decorator', () => { | ||
context('proposal 1', () => { | ||
@model() | ||
class Product extends Entity { | ||
@property({ | ||
type: 'string', | ||
}) | ||
name: string; | ||
@belongsTo(() => Category) | ||
categoryId: number; | ||
|
||
constructor(data?: Partial<Product>) { | ||
super(data); | ||
} | ||
} | ||
|
||
/** | ||
* Navigation properties of the Product model. | ||
*/ | ||
interface ProductRelations { | ||
category?: CategoryWithRelations; | ||
} | ||
/** | ||
* Product's own properties and navigation properties. | ||
*/ | ||
type ProductWithRelations = Product & ProductRelations; | ||
|
||
@model() | ||
class Category extends Entity { | ||
@hasMany(() => Product) | ||
products?: Product[]; | ||
} | ||
/** | ||
* Navigation properties of the Category model. | ||
*/ | ||
interface CategoryRelations { | ||
products?: ProductWithRelations[]; | ||
} | ||
/** | ||
* Category's own properties and navigation properties. | ||
*/ | ||
type CategoryWithRelations = Category & CategoryRelations; | ||
|
||
it('create - generates schema with excluded properties', () => { | ||
const requestBodySpec = { | ||
description: 'Create a product', | ||
required: true, | ||
}; | ||
|
||
const excludeOptions = { | ||
[TS_TYPE_KEY]: Product, | ||
schemaName: 'ProductWithoutID', | ||
exclude: ['id'] | ||
} | ||
|
||
class MyController1 { | ||
@post('/Product') | ||
create(@newRequestBody1( | ||
requestBodySpec, | ||
excludeOptions | ||
) product: Exclude<Product, ['id']>) { } | ||
} | ||
|
||
const spec1 = getControllerSpec(MyController1) | ||
|
||
const requestBodySpecForCreate = spec1.paths[ | ||
'/Product' | ||
]['post'].requestBody; | ||
|
||
const referenceSchema = spec1.components!.schemas!.ProductWithoutID; | ||
|
||
expect(requestBodySpecForCreate).to.have.properties({ | ||
description: 'Create a product', | ||
required: true, | ||
content: { | ||
'application/json': { | ||
schema: { | ||
$ref: '#/components/schemas/ProductWithoutID' | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// The feature that generates schemas according to | ||
// different options is TO BE DONE and out of the | ||
// scope of this spike, so that the schema `PartialProduct` | ||
// here is still the same as `Product` | ||
expect(referenceSchema).to.have.properties({ | ||
title: 'ProductWithoutID', | ||
properties: { | ||
categoryId: { type: 'number' }, | ||
name: { type: 'string' } | ||
} | ||
}); | ||
}) | ||
|
||
it('update - generates schema with partial properties', () => { | ||
const requestSpecForUpdate = { | ||
description: 'Update a product', | ||
required: true, | ||
}; | ||
|
||
const partialOptions = { | ||
[TS_TYPE_KEY]: Product, | ||
schemaName: 'PartialProduct', | ||
partial: true | ||
} | ||
|
||
class MyController2 { | ||
@put('/Product') | ||
update(@newRequestBody1( | ||
requestSpecForUpdate, | ||
partialOptions | ||
) product: Partial<Product>) { } | ||
} | ||
|
||
const spec2 = getControllerSpec(MyController2) | ||
|
||
const requestBodySpecForCreate = spec2.paths[ | ||
'/Product' | ||
]['put'].requestBody; | ||
|
||
const referenceSchema = spec2.components!.schemas!.PartialProduct; | ||
|
||
expect(requestBodySpecForCreate).to.have.properties({ | ||
description: 'Update a product', | ||
required: true, | ||
content: { | ||
'application/json': { | ||
schema: { | ||
$ref: '#/components/schemas/PartialProduct' | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// The feature that generates schemas according to | ||
// different options is TO BE DONE and out of the | ||
// scope of this spike, so that the schema `PartialProduct` | ||
// here is still the same as `Product` | ||
expect(referenceSchema).to.have.properties({ | ||
title: 'PartialProduct', | ||
properties: { | ||
categoryId: { type: 'number' }, | ||
name: { type: 'string' } | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.