Mike R
Mike R

Reputation: 4538

Spring Data Rest: How can I make a json schema property required?

From reading another question, I learned that Spring Data Rest exposes a JSON schema at: /{resourceName}/schema. Ex:

GET http://localhost:8080/members/schema
Accept : application/schema+json
{
    "name": "org.jxc876.model.Member",
    "description": "rest.description.member",
    "links": [],
    "properties": {
        "id": {
            "type": "long",
            "description": "rest.description.member.id",
            "required": false
        },
        "alias": {
            "type": "string",
            "description": "rest.description.member.alias",
            "required": false
        },
        "name": {
            "type": "string",
            "description": "rest.description.member.name",
            "required": false
        }
    }
}

I want to indicate that certain fields are required. I initially tried using Bean Validation annotations (@NotNull) and Jackson (@JacksonProperty) annotations but neither seemed to trigger the flag.

Note: I am using Spring Data Rest 2.2.1

Upvotes: 5

Views: 4069

Answers (1)

Mike R
Mike R

Reputation: 4538

I looked into the source code, and found:

  • RepositorySchemaController - maps to /{repository}/schema
  • PersistentEntityToJsonSchemaConverter
    • contains a convert(Object, TypeDescriptor, TypeDescriptor) method which creates the Json schema
    • Uses another class JsonSchema and its nested inner class (Property)
    • The Property contains 3 fields: type, description, required
    • It looks like the constructor call is hardcoded to always use false: new Property(type, message, false)

I'm thinking I should extend PersistentEntityToJsonSchemaConverter and override the convert method, then also override the controller and call my custom JsonSchemaConverter.

Upvotes: 1

Related Questions