mia-platform / crud-service

A lightweight application to expose restful CRUD API over HTTP interface
https://docs.mia-platform.eu/docs/how_to/crud_service/crud_oss_usage
Apache License 2.0
43 stars 4 forks source link

/schema Returns Incomplete JSON Schema Missing Required Fields Information #326

Closed GioAc96 closed 2 months ago

GioAc96 commented 3 months ago

Description Good afternoon. I am trying to use the /schema endpoint to retrieve the schema of a collection. While the endpoint returns a valid JSON Schema, the "required" field of the schema is missing, making it incomplete. We rely on this functionality to validate a set of documents before deleting all documents in a collection and replacing them, but the supplied schema is not sufficient to correctly validate our documents before wiping the collection.

Environment

I'm using CRUD Service version 7.0.2

Minimal Reproduction

Given a crud-service instance with a collection books that has the required field name, the following curl

curl --location 'http://crud-service/books/schema'

returns the following schema

{
    "type": "object",
    "properties": {
        "_id": {
            "type": "string",
            "description": "_id",
            "pattern": "^[a-fA-F\\d]{24}$",
            "example": "000000000000000000000000"
        },
        "creatorId": {
            "type": "string",
            "description": "creatorId"
        },
        "createdAt": {
            "type": "string",
            "example": "1997-04-24T07:00:00.000Z",
            "nullable": false,
            "description": "createdAt"
        },
        "updaterId": {
            "type": "string",
            "description": "updaterId"
        },
        "updatedAt": {
            "type": "string",
            "example": "1997-04-24T07:00:00.000Z",
            "nullable": false,
            "description": "updatedAt"
        },
        "__STATE__": {
            "type": "string",
            "description": "__STATE__"
        },
        "name": {
            "type": "string"
        }
    }
}

However, the schema should be:

{
    "type": "object",
    "properties": {
        "_id": {
            "type": "string",
            "description": "_id",
            "pattern": "^[a-fA-F\\d]{24}$",
            "example": "000000000000000000000000"
        },
        "creatorId": {
            "type": "string",
            "description": "creatorId"
        },
        "createdAt": {
            "type": "string",
            "example": "1997-04-24T07:00:00.000Z",
            "nullable": false,
            "description": "createdAt"
        },
        "updaterId": {
            "type": "string",
            "description": "updaterId"
        },
        "updatedAt": {
            "type": "string",
            "example": "1997-04-24T07:00:00.000Z",
            "nullable": false,
            "description": "updatedAt"
        },
        "__STATE__": {
            "type": "string",
            "description": "__STATE__"
        },
        "name": {
            "type": "string"
        }
    },
    "required": [
        "name"
    ]
}
flower-of-the-bridges commented 2 months ago

Hi @GioAc96 thanks for opening the issue, it is indeed a bug.

I've opened a PR addressing this issue, soon it will be merged into main branch