0

im struggling with the mapping to openapi spec of relations in loopback 4.

There are two entities: Plan & PlanItem with relates with "Plan hasMany PlanItems" In my endpoint spec i've declared the inclusion of the relation like that:

  @authenticate('jwt')
  @get('/{id}', {
    responses: {
      '200': {
        description: 'Plan for given id, including items',
        content: {
          'application/json': {
            schema: getModelSchemaRef(Plan, {
              title: "PlanWithRelations",
              exclude: ['ownerId'],
              includeRelations: true
            })
          },
        },
      },
    },
  })
  async findOne(
    @param.path.string('id') id: string,
  ): Promise<Plan | null> {
    return this.plansService.findOne(id);
  }

the resulting components section in the openapi.json looks like this:

{
  "openapi": "3.0.0",
  "info": {
    "title": "ngbm-api - NGBM API v1.0",
    "version": "1.0.0",
    "contact": {

    }
  },
  // ...
  "components": {
    "schemas": {
      // ...
      "PlanWithRelations": {
        "title": "PlanWithRelations",
        "description": "(Schema options: { title: 'PlanWithRelations', exclude: [ 'ownerId' ], includeRelations: true })",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/PlanWithRelations"
            }
          }
        },
        "required": [
          "name"
        ],
        "additionalProperties": false
      },
    }
  }
}

as you can see the items array in the 'PlanWithRelations' has a $ref to the same schema 'PlanWithRelations' and not to 'PlanItem'

Can someone give me a hint how to fix this ? If you need more informations feels free to give me a hint.

here are my loopback --version output:

@loopback/cli version: 2.2.1

@loopback/* dependencies:
  - @loopback/authentication: ^4.1.1
  - @loopback/boot: ^2.0.2
  - @loopback/build: ^5.0.0
  - @loopback/context: ^3.2.0
  - @loopback/core: ^2.2.0
  - @loopback/metadata: ^2.0.2
  - @loopback/openapi-spec-builder: ^2.0.2
  - @loopback/openapi-v3: ^3.1.1
  - @loopback/repository-json-schema: ^2.0.2
  - @loopback/repository: ^2.0.2
  - @loopback/rest: ^3.1.0
  - @loopback/testlab: ^2.0.2
  - @loopback/docs: ^3.2.1
  - @loopback/example-hello-world: ^2.0.2
  - @loopback/example-log-extension: ^2.0.2
  - @loopback/example-rpc-server: ^2.0.2
  - @loopback/example-todo: ^3.0.2
  - @loopback/example-soap-calculator: ^2.0.2
  - @loopback/service-proxy: ^2.0.2
  - @loopback/http-caching-proxy: ^2.0.2
  - @loopback/http-server: ^2.0.2
  - @loopback/example-todo-list: ^3.0.2
  - @loopback/dist-util: ^0.4.0
  - @loopback/rest-explorer: ^2.0.2
  - @loopback/eslint-config: ^6.0.2
  - @loopback/example-express-composition: ^2.0.2
  - @loopback/example-greeter-extension: ^2.0.2
  - @loopback/booter-lb3app: ^2.0.2
  - @loopback/example-lb3-application: ^2.0.2
  - @loopback/example-greeting-app: ^2.0.2
  - @loopback/example-context: ^2.0.2
  - @loopback/repository-tests: ^0.11.2
  - @loopback/extension-health: ^0.3.2
  - @loopback/authorization: ^0.5.2
  - @loopback/rest-crud: ^0.7.2
  - @loopback/security: ^0.2.2
  - @loopback/authentication-passport: ^2.0.2
  - @loopback/example-metrics-prometheus: ^0.2.2
  - @loopback/extension-metrics: ^0.2.2
  - @loopback/model-api-builder: ^2.0.2
  - @loopback/extension-logging: ^0.2.2
  - @loopback/example-access-control-migration: ^1.1.2
  - @loopback/example-file-transfer: ^1.1.2
  - @loopback/example-rest-crud: ^1.1.1
  - @loopback/apiconnect: ^0.1.1
  - @loopback/example-validation-app: ^1.1.1
  - @loopback/cron: ^0.1.0

Peter C. Glade
  • 543
  • 2
  • 8
  • 16

1 Answers1

1

It is a known bug that including a "title" will cause invalid schema generation.

A workaround is to remove the "title" key from getModelSchemaRef:

schema: getModelSchemaRef(Plan, {
  exclude: ['ownerId'],
  includeRelations: true
})

The PR with the fix has been merged and the fix will be released soon.

Rifa Achrinza
  • 1,555
  • 9
  • 19
  • Yes I've raised the issue on strongloop/loopback-next after I was sure who is the best one to fix this issue. I just forgot to link this here. I've create another workaround by adapting the getModelSchemaRef function and integrated a way to override the generated properties. [first version mentioned here](https://github.com/strongloop/loopback-next/issues/5088#issuecomment-612366448) But at least it was fixed and merged ;) – Peter C. Glade Apr 16 '20 at 19:37