Search code examples
loopbackjsloopback

Loopback 4 The swagger generated filters don't seem to work


I set up a basic model and controller connected to mysql database. Everything looks good and is working.

My Definition Model looks like this.

import { Entity, model, property } from '@loopback/repository';

@model({
  settings: {
    mysql: {
      table: 'definition'
    }
  }
})
export class Definition extends Entity {
  @property({
    type: 'number',
    id: true,
    generated: true,
    forceId: true,
    required: false,
    description: 'The unique identifier for a definition',
  })
  id: number;

  @property({
    type: 'string',
    required: true,
  })
  name: string;

  @property({
    type: 'string',
    required: true,
  })
  process_id: string;


  constructor(data?: Partial<Definition>) {
    super(data);
  }
}

export interface DefinitionRelations {
  // describe navigational properties here
}

export type DefinitionWithRelations = Definition & DefinitionRelations;

When I have it up and running and click on the explorer to test it out.

I click on "Try it out" for get /definitions and the only editable field that gets enabled is the filter field.

It is repopulated with a value like this...

 {
  "where": {},
  "fields": {
    "id": true,
    "name": true,
    "process_id": true
  },
  "offset": 0,
  "limit": 0,
  "skip": 0,
  "order": [
    "string"
  ]
}

I have to clear that to get it to work which is fine. When I run it with no filter it returns these results.

[
  {
    "id": 10,
    "name": "Place Manual Payoff Order Process Config",
    "process_id": "Process_PlaceManualPayoffOrderProcessConfig"
  },
  {
    "id": 11,
    "name": "test",
    "process_id": "Test"
  },
  {
    "id": 12,
    "name": "test2",
    "process_id": "test2"
  }
]

I am trying to use the filter expression to only return ones with a specific process_id field. So I change the filter to look like this.

{
  "where": {"process_id": "test2"}
}

And it still returns the same results.

[
  {
    "id": 10,
    "name": "Place Manual Payoff Order Process Config",
    "process_id": "Process_PlaceManualPayoffOrderProcessConfig"
  },
  {
    "id": 11,
    "name": "test",
    "process_id": "Test"
  },
  {
    "id": 12,
    "name": "test2",
    "process_id": "test2"
  }
]

Do filters currently work in Loopback 4 or am I using them incorrectly?

EDIT: if I post the filters in the URL string they work. It seems as though the openapi ui isn't generating that part of the filter Into the URL string.


Solution

  • It seems as though the openapi ui isn't generating that part of the filter Into the URL string.

    Yes, this a precise description.

    OpenAPI Spec version 3.x does not specify how to serialize deeply-nested values to URL queries and swagger-js, the library powering swagger-ui (which powers LoopBack's REST API Explorer), silently ignores such values.

    The issue has been reported and is being discussed here: