Skip to content

Optional Fields are not filtered out with default condition. #1647

@Quad-HK

Description

@Quad-HK

Issue

When querying with the default condition "=" on an optional field, records without this field appear in the result list.

Expected Result

Only records which contain that optional field and matching its value to the condition appear in the result list.

Example

db.json:

{
    "test-records": [
        {
            "id": "1",
            "name": "test 1 with optional field A",
            "optionalField": "A"
        },
        {
            "id": "2",
            "name": "test 2 with optional field B",
            "optionalField": "B"
        },
        {
            "id": "3",
            "name": "test 3 without optional field"
        }
    ]
}

Request:
http://localhost:3000/test-records?optionalField=A

Current Response:

[
  {
    "id": "1",
    "name": "test 1 with optional field A",
    "optionalField": "A"
  },
  {
    "id": "3",
    "name": "test 3 without optional field"
  }
]

json-server version: 1.0.0-beta.3

Possible code location of issue

At json-server/src/service.ts in the block starting at line 300 (case Condition.default:), itemValue is not tested for undefined and therefor code is processed until line 312 which returns true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions