Skip to content

Question: is schema information embeddable with the instances (actual data)? #660

Closed
@anatoli26

Description

@anatoli26

Sorry if this is something trivial and explained the docs, I'm trying to understand JSON (Hyper-)Schema, but it looks like I'm missing the most important aspect of how to utilize JSON Schema & Hyper-Schema in practice for RESTful APIs.

My doubt is whether the schema information is embeddable into the instance data replies (returned by the server) or they coexist in parallel? All the examples I've seen so far show the instance data and the scheme describing the corresponding entities in separate structures. Is it the only way possible?

Say, the client sends a request: GET /users, my typical JSON reply would be:

{
    "users": [
        {
            "id": 1,
            "name": "John Doe",
            "country: "Sweden",
        },
        {
            "id": 2,
            "name": "Jane Doe",
            "country: "Scotland",
        }
    ]
}

So, my doubts are:

  1. How should I indicate that each element of the array is in fact an instance of user entity with a particular JSON Schema definition (the schema definition being either embedded in the results or provided as a URI)?
  2. How should I indicate (in the hypermiedia/HATEOAS sense) that only the 2nd item is editable (i.e. PUT/DELETE allowed on the 2nd item, but not allowed on the 1st)? Is Hyper-Schema a hypermedia format? Could it be used like e.g. HAL or Mason with dynamic links based on each query conditions?

With respect to the 1st question, I understand how to construct JSON Schemas. What's not clear to me whether the JSON Schemas are a static data that exists independently of the instance data and the client should first consult the schema and only after that it will know where to get pure instance data and how to interpret it OR there exists a way to embed the schema information (via links or other ways) to indicate which entities the instance data objects correspond to?

With respect to the 2nd question, my doubt is similar, i.e. are the hyper-schema definitions also static data and the possible actions (via "targetHints": { "allow": [...] }) should be managed via if then else conditions on certain properties (so I'd have to add special ad-hoc properties indicating if the instance is read-only or read-write) OR is there a way to embed this information (again, probably via links) into the actual data, like with Mason or Hydra?

In other words, is it possible to return with JSON Hyper-Schema something equivalent to the following?

{
    "users": [
        {
            "id": 1,
            "name": "John Doe",
            "country: "Sweden",
            "links": [
                {
                    "href": "/users/1",
                    "rel": "self",
                    "method": "GET"
                },
            ]
        },
        {
            "id": 2,
            "name": "Jane Doe",
            "country: "Scotland",
            "links": [
                {
                    "href": "/users/2",
                    "rel": "self",
                    "method": "GET"
                },
                {
                    "href": "/users/2",
                    "rel": "edit",
                    "method": "PUT"
                },
                {
                    "href": "/users/2",
                    "rel": "delete",
                    "method": "DELETE"
                }
            ]
        }
    ],
    "links": [
        {
            "href": "/users",
            "rel": "create",
            "method": "POST"
        }
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions