loopbackio / loopbackio/loopback-next

getModelSchemaRef doesn't get all extended model's properties.

Open
#7,566 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug OpenAPI
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

## Steps to reproduce

I've got a controller with an endpoint which take a request body of the `Foo` model class.
```ts
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Foo),
},
},
})
```

The model classes:
```ts
@model()
export class BarBase {
@property()
name: string
}

@model()
export class BarEdit extends BarBase {
@property()
id: number
@property()
delete: boolean
}

@model()
export class Foo {
@property()
id: number
@property().array(BarEdit)
bars: BarEdit[]
}

@model()
export class BarResult extends BarEdit {
@property()
result: string
}
```

## Current Behaviour

When looking at the generated swagger document or calling the controller endpoint, the schema body is the following:
```ts
{
id: number,
bars: [
{
name: string
}
]
}
```
The `id` and `delete` properties are missing.

## Expected Behaviour

I expected that the controller model schema of `Foo` would be:
```ts
{
id: number,
bars: [
{
id: number,
delete: boolean,
name: string
}
]
}
```

In the current behaviour the `BarEdit` model class seems to extends from the `BarBase` class and it has got the `name` property, but the `id` and `delete` property in the `BarEdit` model class itself disappeared. I expected the `id` and `delete` properties to be there as well, but it's not.

Also when the model class `BarResult` (which extends from `BarEdit`) is used in a controller's `getModelSchemaRef` it has got all the properties from all 3 model classes:
```ts
{
id: number,
delete: boolean,
name: string,
result: string
}
```

## Additional information

A work around to this problem I've found is to create a copy of the `BarBase` model class and call it something for example `BarBaseT` and have `BarEdit` extends from that one instead.

## Operating System Information

```sh
darwin x64 14.15.4
```

```sh
├── @loopback/boot@3.4.0
├── @loopback/core@2.16.0
├── @loopback/repository@3.6.0
├── @loopback/rest-explorer@3.3.0
├── @loopback/rest@9.3.0
├── @loopback/service-proxy@3.2.0
├── loopback-connector-kv-redis@3.0.3
├── loopback-connector-postgresql@5.3.0
├── loopback-connector-rest@3.7.0
npm notice
npm notice New minor version of npm available! 7.6.1 -> 7.16.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v7.16.0
npm notice Run npm install -g npm@7.16.0 to update!
npm notice
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the BarBase, BarEdit, BarResult, and Foo model definitions and the controller getModelSchemaRef usage shown in the report. Trace the generated schema for Foo and compare it with the expected inherited properties; done when BarEdit's id and delete properties appear in the nested schema without the workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.