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): use correct behavior for hidden properties #10698

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -5,20 +5,20 @@

import {MetadataInspector, Reflector} from '@loopback/core';
import {
Entity,
MODEL_KEY,
ModelDefinitionSyntax,
PropertyType,
belongsTo,
Entity,
hasMany,
model,
MODEL_KEY,
ModelDefinitionSyntax,
property,
PropertyType,
} from '@loopback/repository';
import {expect} from '@loopback/testlab';
import {
getJsonSchema,
JSON_SCHEMA_KEY,
JsonSchema,
getJsonSchema,
modelToJsonSchema,
} from '../..';
import {expectValidJsonSchema} from '../helpers/expect-valid-json-schema';
Expand All @@ -32,6 +32,7 @@ describe('build-schema', () => {
// it's possible to reproduce the problematic edge case by
// creating the model definition object directly.
class TestModel {}

const definition: ModelDefinitionSyntax = {
name: 'TestModel',
properties: {
Expand All @@ -51,6 +52,7 @@ describe('build-schema', () => {
// it's possible to reproduce the problematic edge case by
// creating the model definition object directly.
class TestModel {}

const definition: ModelDefinitionSyntax = {
name: 'TestModel',
properties: {
Expand Down Expand Up @@ -1173,6 +1175,7 @@ describe('build-schema', () => {
@hasMany(() => Product)
products?: Product[];
}

const expectedSchema: JsonSchema = {
definitions: {
ProductWithRelations: {
Expand Down Expand Up @@ -1429,6 +1432,179 @@ describe('build-schema', () => {
});
});

context(
'hides properties when model setting "hiddenProperties" is set',
() => {
@model({
settings: {
hiddenProperties: [],
},
})
class FullProduct extends Entity {
@property({id: true, required: true})
id: number;

@property()
name: string;

@property()
description: string;
}

@model({
settings: {
hiddenProperties: ['name'],
},
})
class SingleHiddenPropertyModelProduct extends Entity {
@property({id: true, required: true})
id: number;

@property()
name: string;

@property()
description: string;
}

@model({
settings: {
hiddenProperties: ['name', 'description'],
},
})
class MultipleHiddenPropertyModelProduct extends Entity {
@property({id: true, required: true})
id: number;

@property()
name: string;

@property()
description: string;
}

@model()
class SingleHiddenPropertyProduct extends Entity {
@property({id: true, required: true})
id: number;

@property({
hidden: true,
})
name: string;

@property()
description: string;
}

@model()
class MultipleHiddenPropertyProduct extends Entity {
@property({id: true, required: true})
id: number;

@property({
hidden: true,
})
name: string;

@property({
hidden: true,
})
description: string;
}

it('excludes one property when the model settings "hiddenProperties" is set to hide a single property', () => {
const originalSchema = getJsonSchema(FullProduct);
expect(originalSchema.properties).to.deepEqual({
id: {type: 'number'},
name: {type: 'string'},
description: {type: 'string'},
});
expect(originalSchema.title).to.equal('FullProduct');

const hideNameSchema = getJsonSchema(
SingleHiddenPropertyModelProduct,
);
expect(hideNameSchema).to.deepEqual({
title: 'SingleHiddenPropertyModelProduct',
type: 'object',
properties: {
id: {type: 'number'},
description: {type: 'string'},
},
required: ['id'],
additionalProperties: false,
});
});

it('excludes multiple properties when the model settings "hiddenProperties" is set to hide multiple properties', () => {
const originalSchema = getJsonSchema(FullProduct);
expect(originalSchema.properties).to.deepEqual({
id: {type: 'number'},
name: {type: 'string'},
description: {type: 'string'},
});
expect(originalSchema.title).to.equal('FullProduct');

const hideNameSchema = getJsonSchema(
MultipleHiddenPropertyModelProduct,
);
expect(hideNameSchema).to.deepEqual({
title: 'MultipleHiddenPropertyModelProduct',
type: 'object',
properties: {
id: {type: 'number'},
},
required: ['id'],
additionalProperties: false,
});
});

it('excludes one property when the property setting is set to hidden', () => {
const originalSchema = getJsonSchema(FullProduct);
expect(originalSchema.properties).to.deepEqual({
id: {type: 'number'},
name: {type: 'string'},
description: {type: 'string'},
});
expect(originalSchema.title).to.equal('FullProduct');

const hideNameSchema = getJsonSchema(SingleHiddenPropertyProduct);
expect(hideNameSchema).to.deepEqual({
title: 'SingleHiddenPropertyProduct',
type: 'object',
properties: {
id: {type: 'number'},
description: {type: 'string'},
},
required: ['id'],
additionalProperties: false,
});
});

it('excludes multiple properties when the property settings are set to hide multiple properties', () => {
const originalSchema = getJsonSchema(FullProduct);
expect(originalSchema.properties).to.deepEqual({
id: {type: 'number'},
name: {type: 'string'},
description: {type: 'string'},
});
expect(originalSchema.title).to.equal('FullProduct');

const hideNameSchema = getJsonSchema(MultipleHiddenPropertyProduct);
expect(hideNameSchema).to.deepEqual({
title: 'MultipleHiddenPropertyProduct',
type: 'object',
properties: {
id: {type: 'number'},
},
required: ['id'],
additionalProperties: false,
});
});
},
);

context('optional properties when option "optional" is set', () => {
@model()
class Product extends Entity {
Expand Down
13 changes: 13 additions & 0 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,19 @@ export function modelToJsonSchema<T extends object>(
continue;
}

if (meta?.settings?.hiddenProperties?.includes(p)) {
debug(
'Property % is hidden by model hiddenProperties settings %s',
meta?.settings?.hiddenProperties?.includes(p),
);
continue;
}

if (meta.properties[p].hidden) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a model property has a hidden property, it is added to meta.settings.hiddenProperties. You probably don't need this check. The first condition should be enough.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried removing the check, and I'm not seeing the behavior you described.
The 'HiddenProperty' tests are now failing with the properties in the schema not removed. I tried debugging the unit test excludes one property when the property setting is set to hidden and I'm seeing the SingleHiddenPropertyProduct having no meta.settings sub-properties available

image

Copy link
Author

@taylorcoffelt taylorcoffelt Nov 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like that behavior only happens in packages/repository/src/decorators/model.decorator.ts / buildModelDefinition.

This is run once on the start, but when the modelToJsonSchema is called, the ModelMetadataHelper.getModelMetadata is unable to locate the cached model. This causes it to rebuild the model meta (without the behavior from the buildModelDefinition that adds the hidden properties to the model's settings.hiddenProperties)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @taylorcoffelt. You are right. meta.settings.hiddenProperties is not populated with hidden properties here.

This change not only hides the properties from the response, it hides from the request body example too - which it shouldn't as we can post with hidden fields.

Secondly, the filters still show the hidden fields.

You can hide properties in the filter by filtering hidden fields here.

const hiddenProperties = modelCtor.definition.settings.hiddenProperties || [];
const originalProps = Object.keys(modelCtor.definition.properties);
const properties = originalProps.filter(prop => !hiddenProperties.includes(prop));

While you can remove the hidden properties from the responses in /rest-crud/src/crud-rest.controller.ts.

    @get('/{id}', {
      ...response.model(200, `${modelName} instance`, modelCtor, {
        includeRelations: true,
      }),
    })

We can pass exclude as an array along with includeRelations.

debug('Property % is hidden by model property hidden setting', p);
continue;
}

if (meta.properties[p].type == null) {
// Circular import of model classes can lead to this situation
throw new Error(
Expand Down
Loading