Error Including Trait from External File


#1

I have 2 simple files in my project (which is designed to test the new MuleSoft JWT Validation policy).

I have a simple main RAML file in the ‘root’ folder:

#%RAML 1.0
version: v1
title: #removedFromCodeSample
baseUri: #removedFromCodeSample
uses: 
  jwt: !include traits/jwt.raml

/check:
  get:
    is: [jwt.jwt]
    headers:
      token: string
    responses:
      200:
        headers:
          x-oauth-policy: string

and a trait in an external file at traits/jwt.raml

#%RAML 1.0
title: jwt

traits:
  jwt:
    headers:
      authorization:
        description: Bearer <JWT>
        type: string
    responses:
      400:
        description: Token was not provided.
      401:
        description: Bad or expired token. To fix, you should re-authenticate the user.
      403:
        description: The client id validation failed.
      503:
        description: Error communicating with JWKS server.

(everything from the word “traits” down is directly copy-pasted from Anypoint’s instructions!)

This is fine in Anypoint Design Center, but when I try to build it in MuleSoft’s Anypoint Studio it throws an error saying that “The included resource /traits/jwt.raml contains errors”

This is obviously attached to the main RAML file that includes the trait file, however there are no errors shown in the trait file itself. When I paste the trait into the main file, it works fine. Is this a problem with the parser/studio application or have I done something wrong in my RAML?


#2

Your file containing the trait is missing a fragment identifier. For traits, you can either use #%RAML 1.0 Trait (single trait) or #%RAML 1.0 Library. Here’s the section of the spec about Typed Fragments.


#3

Thanks - this started me off on the right track. I ended up with quite a few changes in order to make it work.

1: As you say, change line 1 in the jwt.raml file to #%RAML 1.0 Trait
2: Remove the “title:” and “traits:” and “jwt:” properties from the Trait file (and re-align the headers, responses etc. properties)
3: Change the “uses:” property in the main RAML file to “traits:”
4: Due to 2, change is: [jwt.jwt] to simply is: [jwt]