Skip to content

Conversation

@taylorcoffelt
Copy link

Update emitted json schema to always omit properties marked as hidden at both model and property levels.

This also affects schema emitted for related models.

Currently, hidden properties are only omitted at the top level in the model schema. If a related model was included as part of the emitted schema, the hiddenbehavior on the model or model properties would be ignored, and the entire nested schema emitted, defeating the purpose of the hidden fields, and requiring additional models / extra work to omit them from the emitted schema.

Potentially Fixes #7517
Fixes #4495
Fixes #1914

Checklist

  • DCO (Developer Certificate of Origin) signed in all commits
  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

Update emitted json schema to always omit properties marked as hidden
 at both model and property levels

Signed-off-by: Taylor Coffelt <[email protected]>
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Loopback 4: Exclude issue in hasone relations OpenAPI shema definition for models should exclude hidden properties by default Hidden properties

2 participants