I have an API definition only with GET methods. Now I'm trying to reuse data types definitions to define new POST methods.
My data type conains and array of a object with some fields. Some of this fields are for both methods, one field only to POST, and some fields only to GET method.
I try it with Libraries: the first file ( library_1.raml ) contains all the fields. In the second file ( library_2.raml ) I try to inherit only the fields I need but I have this error:
Resolution error: Invalid scalar inheritance base type http://www.w3.org/2001/XMLSchema#string < http://www.w3.org/2001/XMLSchema#integer
Then I try it with Data Types. I create 3 files: dataType1.raml contains the fields that need both methods ( GET and POST ) defined as properties.
dataType2.raml contains array definition, the property only for POST and a include to dataType1.raml
dataType3.raml contains array definition, the properties for GET method and a include to dataType3.raml
This approach seems to work, but the object properties has no order.
What option do you think is the best?
Regards,
Josep
Hello @Josep Campos Gadea
When defining APIs, especially with RAML, it's crucial to use modular design principles to maximize reusability and maintainability. Given the problem you're facing, I can provide some guidelines to achieve your goal. Here's a breakdown:
- Use Libraries:
Libraries in RAML are specifically designed for reusability. You can define data types, traits, and other RAML components within a library and then include them where needed.
2.Data Types Inheritance:
RAML supports inheritance for data types. If you have a base data type with fields common to both GET and POST methods, you can define it as your base data type. Then, extend this base type for GET-specific and POST-specific properties.
#%RAML 1.0 DataType
title: BaseDataType
properties:
commonProperty1: string
commonProperty2: number
For Get:
#%RAML 1.0 DataType
title: GetDataType
type: BaseDataType
properties:
getProperty: boolean
For POST:
#%RAML 1.0 DataType
title: PostDataType
type: BaseDataType
properties:
postProperty: string
Resolution Error:
- The error Resolution error: Invalid scalar inheritance base type you're getting suggests a type mismatch. This usually happens when you're trying to inherit or override a property that's of a different type than what's defined in the base. Double-check the types to ensure they match or are compatible.
It's important to note that, in JSON or YAML, the order of properties in an object does not typically matter. If you're seeing them out of order in generated documentation or some tool, it might be due to how that tool processes the RAML. If order is critical for your use case, you might need to look for ways to customize the output or display of the tool you're using.
Given your requirements, I would recommend going with the Data Types inheritance approach. It's clean, modular, and aligns well with RAML's design principles. Ensure that you handle the inheritance correctly and avoid type mismatches.