loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

OpenAPI shema definition for models should exclude hidden properties by default

Open ih84ds opened this issue 6 years ago • 4 comments

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.

ih84ds avatar Jan 24 '20 16:01 ih84ds

@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 avatar Jan 30 '20 21:01 jannyHou

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']}),
          },
      },
    

jannyHou avatar Jan 30 '20 21:01 jannyHou

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?

sertal70 avatar Mar 20 '21 10:03 sertal70

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

tokidoki11 avatar Jun 20 '23 09:06 tokidoki11