-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(repository): introduce resolver and hasMany key inferrence with belongsTo decorator AND add belongsTo repository #1618
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
Changes from all commits
78777cf
1700037
25cb5d8
f0230ce
ec84a0d
bcd7ee9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,13 @@ | |
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {model, property} from '@loopback/repository'; | ||
| import { | ||
| model, | ||
| property, | ||
| belongsTo, | ||
| hasMany, | ||
| Entity, | ||
| } from '@loopback/repository'; | ||
| import { | ||
| modelToJsonSchema, | ||
| JSON_SCHEMA_KEY, | ||
|
|
@@ -370,6 +376,84 @@ describe('build-schema', () => { | |
| expectValidJsonSchema(jsonSchema); | ||
| }); | ||
|
|
||
| it('properly converts decorated custom array type with a resolver', () => { | ||
| @model() | ||
| class CustomType { | ||
| @property() | ||
| prop: string; | ||
| } | ||
|
|
||
| @model() | ||
| class TestModel { | ||
| @property.array(() => CustomType) | ||
| cusType: CustomType[]; | ||
| } | ||
|
|
||
| const jsonSchema = modelToJsonSchema(TestModel); | ||
| expect(jsonSchema.properties).to.deepEqual({ | ||
| cusType: { | ||
| type: 'array', | ||
| items: {$ref: '#/definitions/CustomType'}, | ||
| }, | ||
| }); | ||
| expect(jsonSchema.definitions).to.deepEqual({ | ||
| CustomType: { | ||
| title: 'CustomType', | ||
| properties: { | ||
| prop: { | ||
| type: 'string', | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| expectValidJsonSchema(jsonSchema); | ||
| }); | ||
|
|
||
| it('properly converts decorated models with hasMany and belongsTo', () => { | ||
| @model() | ||
| class Order extends Entity { | ||
| @property({id: true}) | ||
| id: number; | ||
| @belongsTo(() => Customer) | ||
| customerId: number; | ||
| } | ||
|
|
||
| @model() | ||
| class Customer extends Entity { | ||
| @property({id: true}) | ||
| id: number; | ||
| @hasMany(() => Order) | ||
| orders: Order[]; | ||
| } | ||
|
|
||
| const orderSchema = modelToJsonSchema(Order); | ||
| const customerSchema = modelToJsonSchema(Customer); | ||
| expect(orderSchema.properties).to.deepEqual({ | ||
| id: {type: 'number'}, | ||
| customerId: {type: 'number'}, | ||
| }); | ||
| expect(customerSchema.properties).to.deepEqual({ | ||
| id: {type: 'number'}, | ||
| orders: { | ||
| type: 'array', | ||
| items: {$ref: '#/definitions/Order'}, | ||
| }, | ||
| }); | ||
| expect(customerSchema.definitions).to.deepEqual({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add checks for |
||
| Order: { | ||
| title: 'Order', | ||
| properties: { | ||
| id: { | ||
| type: 'number', | ||
| }, | ||
| customerId: {type: 'number'}, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expectValidJsonSchema(orderSchema); | ||
| }); | ||
|
|
||
| it('creates definitions only at the root level of the schema', () => { | ||
| @model() | ||
| class CustomTypeFoo { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ import {expect} from '@loopback/testlab'; | |
| import {isComplexType, stringTypeToWrapper, metaToJsonProperty} from '../..'; | ||
|
|
||
| describe('build-schema', () => { | ||
| class CustomType {} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only used by 2 tests, any reason we shouldn't just declare this within the test
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| describe('stringTypeToWrapper', () => { | ||
| context('when given primitive types in string', () => { | ||
| it('returns String for "string"', () => { | ||
|
|
@@ -76,7 +78,6 @@ describe('build-schema', () => { | |
| }); | ||
|
|
||
| it('returns true if any other wrappers are passed in', () => { | ||
| class CustomType {} | ||
| expect(isComplexType(CustomType)).to.eql(true); | ||
| }); | ||
| }); | ||
|
|
@@ -107,12 +108,17 @@ describe('build-schema', () => { | |
| }); | ||
|
|
||
| it('converts complex types', () => { | ||
| class CustomType {} | ||
| expect(metaToJsonProperty({type: CustomType})).to.eql({ | ||
| $ref: '#/definitions/CustomType', | ||
| }); | ||
| }); | ||
|
|
||
| it('converts complex types with resolver', () => { | ||
| expect(metaToJsonProperty({type: () => CustomType})).to.eql({ | ||
| $ref: '#/definitions/CustomType', | ||
| }); | ||
| }); | ||
|
|
||
| it('converts primitive arrays', () => { | ||
| expect(metaToJsonProperty({type: Array, itemType: Number})).to.eql({ | ||
| type: 'array', | ||
|
|
@@ -121,11 +127,19 @@ describe('build-schema', () => { | |
| }); | ||
|
|
||
| it('converts arrays of custom types', () => { | ||
| class CustomType {} | ||
| expect(metaToJsonProperty({type: Array, itemType: CustomType})).to.eql({ | ||
| type: 'array', | ||
| items: {$ref: '#/definitions/CustomType'}, | ||
| }); | ||
| }); | ||
|
|
||
| it('converts array types with resolver', () => { | ||
| expect( | ||
| metaToJsonProperty({type: Array, itemType: () => CustomType}), | ||
| ).to.eql({ | ||
| type: 'array', | ||
| items: {$ref: '#/definitions/CustomType'}, | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A
TODOcomment to address?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
☝️