Search code examples
muleanypoint-studioraml

RAML showing error when adding trait or resource type and names into trait and resoucre type external files


I am using Mule 4 and Anypoint Studio 7.

I have been refactoring a RAML file to have the Traits and ResourceTypes in their own files so they can be referenced from the main RAML file. The main RAML file highlights the reference in red and states there are errors in the external file and is unclear what the error is.

I have noticed that when I remove the trait: or resourceType: from the external file and also the name for the trait or resourceType it works correctly.

I thought you could add names to your traits and resourceTypes if you wanted too so am I using the incorrect syntax or when you move the traits and resourceTypes outside of the main RAML then each e.g. trait needs its own file?

Example: Resource Type

Causes Error:

#%RAML 1.0 ResourceType
resourceType:
  genericData:
    description: Generic data resource
    is: [headers]
    get:
      description: Get all <<resourcePathName>>
      is: [myDataResponse] 
    post:
      description: Update <<resourcePathName>>
      body:
        application/json:
          example: examples/data.json
      is: [mySuccessResponse]

Error disappears

#%RAML 1.0 ResourceType
    description: Generic data resource
    is: [headers]
    get:
      description: Get all <<resourcePathName>>
      is: [myDataResponse] 
    post:
      description: Update <<resourcePathName>>
      body:
        application/json:
          example: examples/data.json
      is: [mySuccessResponse]

Example: Trait

Causes Error:

#%RAML 1.0 Trait
trait:
  messageResponse:
    responses: 
      200:
        body: 
          application/json:
            example: examples/success.json

Error disappears

#%RAML 1.0 Trait
    responses: 
      200:
        body: 
          application/json:
            example: examples/success.json

Thanks


Solution

  • If a file begins with a RAML fragment identifier line, and the fragment identifier is not Library, Overlay, or Extension, the contents of the file after removal of the RAML fragment identifier line MUST be valid structurally according to the relevant RAML specification.

    So if it's a Trait, no need to add trait: and if it is a ResourceType no need to add resourceType: at the beginning.

    They also cannot be named within fragments. You need to define the name when importing into your raml:

    #%RAML 1.0
    title: My API
    traits:
      messageResponse: !include resourceTypes/messageResponse.raml
    

    You can name them in Libraries though. Something like:

    #%RAML 1.0 Library
    
    traits:
      messageResponse:
        responses: 
          200:
            body: 
              application/json:
                example: examples/success.json
    

    And then you can combine Data Type fragments and Libraries to make it even better. Full example:

    #%RAML 1.0 Trait
        responses: 
          200:
            body: 
              application/json:
                example: examples/success.json
    

    mylib.raml:

    #%RAML 1.0 Library
    
    traits:
      messageResponse: !include traits/messageResponse.raml
    

    myapi.raml:

    #%RAML 1.0
    title: My API
    
    uses:
      myLib: library.raml
    
    /resource:
      is: [ myLib.messageResponse ]
      get: