Reuse schema for POST and PUT methods in RAML(0.8)


#1

Consider the below json which is used in POST and PUT methods,
but the differents are,

  1. POST doesn’t require id property but remaining properties,
  2. PUT required .all properties including id property.
    Is there way to define common schema for POST and PUT.?

    {

     "id": 100,
   "name": "Jhon",
   "age": 26,
   "profession": "Engineer"
}

#2

Yes, in fact what you have could also be a GET response as well. Here is the thing, you would POST the body without the ID, typically… the service would generate the ID usually a UUID or a DB generated ID, and return that as part of the 201 Location header (e.g. /resource/) and possibly as the body as well. In a PUT you would PUT to the /resource/ and include the rest of the body, like a POST. You could also include the ID in the body if you like, although it is more common to pass the ID of the PUT/PATCH call in the URL. In the GET you would return the whole thing as the body, even if you did a GET /resource/ to just get the one resource for the id.

You have the liberty within your service to ignore values passed in. For example, if you accepted a PUT with the full body, you might ignore the value they pass in the body and use the URL id, so as not to modify the ID. Typically the ID is static once generated, but that is up to you as well. Nothing prevents you from changing it. I would argue though that IDs are typically used in table references, so be careful if you are following that practice. But it is entirely up to each and every one of your resource/method handlers on how they handle the incoming body and what to ignore/use.


#3

Hey, Natarajan

Why don’t you use RAML 1.0? It works perfectly for your situation.

You could upgrade your RAML 0.8 to RAML 1.0, not that hard, but then you will be surprised by data type in RAML 1.0.


#4

Hi lijinma,
Yes i achieved it through RAML1.0.
Since am in learning stage of RAML and am having one more doubt.
How can i validate the incoming rest messages in jax-rs ?
Dose RAML/ RAML tools provides any such option to do that ?


#5

Why don’t you use RAML 1.0? It works perfectly for your situation.

  • How this can be acheived, can you provide a sample.

#6

What do you mean validate the incoming rest message? You mean the chunk of JSON that is sent to the API? I guess you could grab the same JSON Schema and use that to validate against the json being sent in?

RAML/RAML Tools would not provide some run time way to handle this, no. RAML defines the API and any tooling for that is around helping define the API using RAML, RAML Types, etc. A generator that takes RAML (and types/schemas) to generate code would be a better bet of having some sort of runtime tools that you could use, but again, I havent seen that either.

What sort of validation are you looking to do? Just make sure it is syntactically correct/formed? Or you looked to ensure default values are set, required fields are not empty, etc? Perhaps also validating the actual values (e.g. length of strings, valid range for numbers, no weird characters, url safe, etc)?