Skip to content

Commit

Permalink
fix(repository-json-schema): fix schema generation for model inheritance
Browse files Browse the repository at this point in the history
Fixes #4721
  • Loading branch information
raymondfeng committed Mar 24, 2020
1 parent 01571a3 commit 5417ed5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,48 @@ describe('build-schema', () => {
// No circular references in definitions
expect(schema.definitions).to.be.undefined();
});

it('allows model inheritance', () => {
@model()
class User {
@property({id: true})
id: string;

@property({
type: 'string',
required: true,
})
name: string;
}

@model()
class NewUser extends User {
@property({
type: 'string',
required: true,
})
password: string;
}

const userSchema = modelToJsonSchema(User, {});
expect(userSchema).to.eql({
title: 'User',
properties: {id: {type: 'string'}, name: {type: 'string'}},
required: ['name'],
additionalProperties: false,
});
const newUserSchema = modelToJsonSchema(NewUser, {});
expect(newUserSchema).to.eql({
title: 'NewUser',
properties: {
id: {type: 'string'},
name: {type: 'string'},
password: {type: 'string'},
},
required: ['name', 'password'],
additionalProperties: false,
});
});
});

describe('getNavigationalPropertyForRelation', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/repository/src/decorators/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export class ModelMetadataHelper {
const classDef = MetadataInspector.getClassMetadata(
MODEL_WITH_PROPERTIES_KEY,
target,
options,
// https://github.com/strongloop/loopback-next/issues/4721
// The `target` can be a subclass for a base model
{...options, ownMetadataOnly: true},
);
// Return the cached value, if it exists.
// XXX(kjdelisle): If we're going to support dynamic updates, then this
Expand All @@ -49,7 +51,7 @@ export class ModelMetadataHelper {
// set ModelDefinition properties if they don't already exist
const meta = new ModelDefinition(Object.assign({}, modelMeta));

// set properies lost from creating instance of ModelDefinition
// set properties lost from creating instance of ModelDefinition
Object.assign(meta, modelMeta);

meta.properties = Object.assign(
Expand Down

0 comments on commit 5417ed5

Please sign in to comment.