Skip to content
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

fix(repository-json-schema): allow recursive model definitions #3674

Merged
merged 1 commit into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// License text available at https://opensource.org/licenses/MIT

import {
Model,
model,
property,
PropertyDefinition,
RelationDefinitionBase,
RelationType,
Expand All @@ -13,6 +16,7 @@ import {
buildModelCacheKey,
getNavigationalPropertyForRelation,
metaToJsonProperty,
modelToJsonSchema,
stringTypeToWrapper,
} from '../..';

Expand Down Expand Up @@ -200,6 +204,39 @@ describe('build-schema', () => {
});
});

describe('modelToJsonSchema', () => {
it('allows recursive model definition', () => {
@model()
class ReportState extends Model {
@property.array(ReportState, {})
states: ReportState[];

@property({
type: 'string',
})
benchmarkId?: string;

@property({
type: 'string',
})
color?: string;

constructor(data?: Partial<ReportState>) {
super(data);
}
}
const schema = modelToJsonSchema(ReportState, {});
expect(schema.properties).to.containEql({
states: {
type: 'array',
items: {$ref: '#/definitions/ReportState'},
},
benchmarkId: {type: 'string'},
color: {type: 'string'},
});
});
});

describe('getNavigationalPropertyForRelation', () => {
it('errors out if targetsMany is undefined', () => {
expect(() =>
Expand Down
2 changes: 1 addition & 1 deletion packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export function modelToJsonSchema<T extends object>(
result.definitions = result.definitions || {};

// promote nested definition to the top level
if (schema.definitions) {
if (result !== schema && schema.definitions) {
for (const key in schema.definitions) {
if (key === title) continue;
result.definitions[key] = schema.definitions[key];
Expand Down