Mulesoft: How to extend Data Types and Override RAML fields

Martin Larizzate – How to extend Data Types and Override RAML fields

RAML Inheritance design

When designing APIs with MuleSoft’s Anypoint Platform, RAML is a powerful tool for defining the structure, behavior, and documentation of APIs. One of the key features that makes RAML flexible and scalable is its support for custom DataTypes, which allow developers to define the shape and format of data being transmitted. However, as API requirements evolve, the need to extend or override existing DataTypes often arises. Whether it’s to add new fields, modify constraints, or reuse and adjust existing definitions for specific cases, understanding how to extend and override DataTypes efficiently is essential for API developers.

In this article, we’ll explore the strategies and best practices for extending and overriding RAML DataTypes in MuleSoft, providing you with practical examples and insights to help streamline your API development process.

At first I will asume you know how Inheritance works on RAML (If not, please check this article) and use a «vehicles» related example.
As you see… there is a «Vechicle» type and two other ones that extend from it.

types:

  Vehicle:
    properties:
      vin: string
      make: string
      model: string
      year: integer
      wheels: integer

  Car:
    type: Vehicle
    properties:
      seatCapacity: integer
      roofRack: boolean

  Truck:
    type: Vehicle
    properties:
      seatCapacity: integer
      haulWeight: integer

What if we need to set a wheels limit to avoid a car with 8 ones?
It’s easier than it looks and we need just to write the vehicle existing field into the child one and place their specific and required values.

types:

  Vehicle:
    properties:
      vin: string
      make: string
      model: string
      year: integer
      wheels: integer

  Car:
    type: Vehicle
    properties:
      seatCapacity: integer
      roofRack: boolean
      wheels: 
        type: integer
        maximum:4
        minimum:4

  Truck:
    type: Vehicle
    properties:
      seatCapacity: integer
      haulWeight: integer
      wheels: 
        type: integer
        maximum:24
        minimum:8

Wrote By:

_- Martin Larizzate -_ Mulesoft Certified Developer, Salesforce Ranger and Salesforce Solution Architect

Deja una respuesta