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

OpenAPI shema definition for models should exclude hidden properties by default #4495

Open
ih84ds opened this issue Jan 24, 2020 · 4 comments · May be fixed by #10698
Open

OpenAPI shema definition for models should exclude hidden properties by default #4495

ih84ds opened this issue Jan 24, 2020 · 4 comments · May be fixed by #10698
Assignees

Comments

@ih84ds
Copy link

ih84ds commented Jan 24, 2020

Hidden properties are automatically removed from the response data (model's toJSON() excludes the hidden properties), so it seems that the schema definition should also exclude these fields by default. Currently, one has to manually add these fields to the "exclude" option in getModelSchemaRef()

Current behavior: hidden properties are removed from response data but openapi schema definition lists hidden properties in the schema.

Expected behavior: hidden properties are removed from response data and openapi schema definition does not list hidden properties.

@jannyHou
Copy link
Contributor

jannyHou commented Jan 30, 2020

@ih84ds Thank you for the suggestion, it sounds reasonable to me. Take the Todo app as an example:

Now the response schema of GET still uses the original model schema Todo

"content": {
      "application/json": {
            "schema": {
                  "$ref": "#/components/schemas/Todo"
             }
       }
}

the Todo schema in reference:

{
  "Todo": {
    "title": "Todo",
    "properties": {
      "id": {
        "type": "number"
      },
      "title": {
        "type": "string"
      },
      "desc": {
        "type": "string"
      },
      // The new added secret property is not hidden
      "secretProp": {
        "type": "string"
      }
    },
    "required": [
      "title"
    ],
    "additionalProperties": false
  }
}

While the expected schema should be

{
  "Todo": {
    "title": "Todo",
    "properties": {
      "id": {
        "type": "number"
      },
      "title": {
        "type": "string"
      },
      "desc": {
        "type": "string"
      }
    },
    "required": [
      "title"
    ],
    "additionalProperties": false
  }
}

@jannyHou
Copy link
Contributor

I can think of two approaches:

  • Generate new schema with hidden properties excluded:

    • In the controller template, introduce a config to hide the properties
      content: {
         'application/json': {
           schema: getModelSchemaRef(Todo, {hiddenProps: true})
         }
      },
    • In @loopback/repository-json-schema, read the flag then generate a new reference for them called TodoExcludeHiddenProps
  • Use the existing exclude config

       content: {
         'application/json': {
            schema: getModelSchemaRef(Todo, {title: 'NewTodo', exclude: ['secretProp']}),
          },
      },

@stale stale bot added the stale label Dec 25, 2020
@sertal70
Copy link

I'm wondering why nobody Is working of this. Excluding hidden properties at controller level is redundant and lead to weirds generated schemas like this:

export interface TodoExcludingSecretPropWithRelations { 
    id?: number;
    title: string;
    desc?: string;
}

@jannyHou do you have any plan to sort it out?

@stale stale bot removed the stale label Mar 20, 2021
@stale stale bot added the stale label Sep 16, 2021
@achrinza achrinza removed the stale label Sep 16, 2021
@loopbackio loopbackio deleted a comment from stale bot Sep 16, 2021
@loopbackio loopbackio deleted a comment from stale bot Sep 23, 2021
@tokidoki11
Copy link

+1 for this,
currently there is no way to remove property in a nested model

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

Successfully merging a pull request may close this issue.

6 participants