-
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: generate schema for relation links
- Loading branch information
Showing
15 changed files
with
232 additions
and
22 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
94 changes: 94 additions & 0 deletions
94
packages/repository-json-schema/src/__tests__/integration/spike.integration.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,94 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/repository-json-schema | ||
// 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 * as Ajv from 'ajv'; | ||
import {JsonSchema, modelToJsonSchema} from '../..'; | ||
|
||
describe('build-schema', () => { | ||
describe('modelToJsonSchema', () => { | ||
it('converts basic model', () => { | ||
@model() | ||
class TestModel { | ||
@property() | ||
foo: string; | ||
} | ||
|
||
const jsonSchema = modelToJsonSchema(TestModel); | ||
expectValidJsonSchema(jsonSchema); | ||
expect(jsonSchema.properties).to.containDeep({ | ||
foo: <JsonSchema>{ | ||
type: 'string', | ||
}, | ||
}); | ||
}); | ||
|
||
it('converts HasMany and BelongsTo relation links', () => { | ||
@model() | ||
class Product extends Entity { | ||
@property({id: true}) | ||
id: number; | ||
|
||
@belongsTo(() => Category) | ||
categoryId: number; | ||
} | ||
|
||
@model() | ||
class Category extends Entity { | ||
@property({id: true}) | ||
id: number; | ||
|
||
@hasMany(() => Product) | ||
products?: Product[]; | ||
} | ||
|
||
const jsonSchema = modelToJsonSchema(Category, {includeRelations: true}); | ||
expectValidJsonSchema(jsonSchema); | ||
expect(jsonSchema).to.deepEqual({ | ||
title: 'CategoryWithLinks', | ||
properties: { | ||
// TODO(bajtos): inherit these properties from Category schema | ||
// See https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/ | ||
id: {type: 'number'}, | ||
products: { | ||
type: 'array', | ||
items: {$ref: '#/definitions/ProductWithLinks'}, | ||
}, | ||
}, | ||
definitions: { | ||
ProductWithLinks: { | ||
title: 'ProductWithLinks', | ||
properties: { | ||
// TODO(bajtos): inherit these properties from Product schema | ||
// See https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/ | ||
id: {type: 'number'}, | ||
categoryId: {type: 'number'}, | ||
category: {$ref: '#/definitions/CategoryWithLinks'}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
function expectValidJsonSchema(schema: JsonSchema) { | ||
const ajv = new Ajv(); | ||
const validate = ajv.compile( | ||
require('ajv/lib/refs/json-schema-draft-06.json'), | ||
); | ||
const isValid = validate(schema); | ||
const result = isValid | ||
? 'JSON Schema is valid' | ||
: ajv.errorsText(validate.errors!); | ||
expect(result).to.equal('JSON Schema is valid'); | ||
} |
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
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
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
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
Oops, something went wrong.