How to make UriParameter as optional in RAML 1.0

1.8k views Asked by At

There is a scenario for example as in any Application Controller while exposing a rest api we can make PathParams/UriParams as optional at controller level, so it wouldn't required always for client to pass it. Now I want to achieve same at RAML level. I can see their documentation that says like this.

"Although a URI parameter MAY be explicitly specified as optional, it SHOULD be required when surrounded directly by slashes (/). In this case, the URI parameter constitutes a complete URI path fragment, for example .../{objectId}/.... It usually makes no sense to allow a URI to contain adjacent slashes, enclosing no characters, for example, ...//.... Therefore, a URI parameter SHOULD be specified as optional only when it appears adjacent to other text. For example, /people/~{fieldSelectors} indicates that URI parameter {fieldSelectors} can be blank, and therefore optional, which implies that /people/~ is a valid relative URI."

That does give sense that we can try with combination of letter e,g /{someLetter}{uriParam} .. at end of resource Url. I did try this way but it always mention that "resouce not found"

Issue is just related to RAML configuration. for example this is sample resource url for which I have to add url param.

/test-api/{testId}

Now I want to keep it the client decision to either pass UriParameter or not.

This is sample RAML code I am trying.

/test-api/{testId}:
    uriParameters:
      testId?: string

/test-api/{testId}:
    uriParameters:
      testId: string
      required: false

/test-api{testId}:
    uriParameters:
      testId: string
 

Now all three approaches aren't working.

1st approach simply making testId as optional using '?'. But if I skip UriParam it shows that no resource found for this.

2nd approach with required: false also not working looks like RAML is ignoring this validation it always expect even a single '/' from me after /test-api

3rd approach isn't working because it again expect me to put UriParam otherwise consider default one.

1

There are 1 answers

0
Marcelo On

To subscribe to the automatic insertion of the URI parameter in RAML 1.0 make this change:

/test-api:
  /{testId}:
    uriParameters:
      testId:
        description: Id of test
        type: string
        required: false
   get:
     description: return test
     responses:
       200:
         body:
           application/json:
             example: { "message": "success" }
   put:
     description: update test
     responses:
       200:
         body:
           application/json:
             example: { "message": "success" }
   delete:
     description: remove test
     responses:
       200:
         body:
           application/json:
             example: { "message": "success" }