I have created a raml in anypoint platform below is my RAML file
#%RAML 0.8
title: es-be-crm-dev
version: 001-dev
#baseUri: http://localhost:8081/api
baseUri: https://mocksvc.mulesoft.com/mocks/44267573-73b2-4369-8e2d- 3a6abbf20e34/api
traits:
- rate-limited:
queryParameters:
client_id:
type: string
client_secret:
type: string
schemas:
- api-request-schema: !include schemas/api-request-schema.json
/teams/{teamId}/teamMembers:
post:
description: Delete Team Member(s) from specified Team
headers:
Accept:
displayName: Accepted response format
description: Caller accepted response content Type
type: string
example: application/json
required: true
default: application/json
Content-Type:
displayName: body Mime Type
description: Caller accepted response content Type
type: string
example: application/json
required: true
default: application/json
body:
application/json:
schema: api-request-schema
example: !include examples/api-request-example.json
responses:
201:
description: Returned when the entity has been successfully created
headers:
ETag:
displayName: Entity Tag
description: Hash of the previously requested resource
type: string
example: asd8asd98hlk209u098asdfmoiun4023
required: false
401:
description: Returned when the user is not authorized to add a member
delete:
description: Adds Team Member(s) in specified Team
headers:
Accept:
displayName: Accepted response format
description: Caller accepted response content Type
type: string
example: application/json
required: true
default: application/json
Content-Type:
displayName: body Mime Type
description: Caller accepted response content Type
type: string
example: application/json
required: true
default: application/json
body:
application/json:
schema: api-request-schema
example: !include examples/api-request-example.json
responses:
201:
description: Returned when the entity has been successfully created
headers:
ETag:
displayName: Entity Tag
description: Hash of the previously requested resource
type: string
example: asd8asd98hlk209u098asdfmoiun4023
required: false
401:
description: Returned when the user is not authorized to delete a member
below is api-request-example.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"teamMembers": {
"type": "array",
"items": {
"type": "object",
"properties": {
"teamMember": {
"type": "object",
"properties": {
"empID": {
"type": "string"
},
"sourceSystemId": {
"type": "string"
},
"sourceSystemObjectId": {
"type": "string"
}
},
"required": [
"empID",
"sourceSystemId",
"sourceSystemObjectId"
]
}
},
"required": [
"teamMember"
]
}
}
},
"required": [
"teamMembers"
]
}
below is Example file
{
"teamMembers": [{
"teamMember": {
"empID": "d3193f53-5f07-e611-80e3-a45d36fc4f90",
"sourceSystemId": "abc",
"sourceSystemObjectId": "abc"
}
}]
}
whenever i try to mock my api in it throws an error with status code 400
{
"error": "schema did not parse: Unexpected token a"
}
is there anything that i am missing here ???
Has the error message says mule is not able to parse your schema and this is probably because the ApiKit version you are using does not support aliases.
That's way he says can't parse a.
Change it with a direct include
schema: !include api-request-schema.json
I know is not ideal but is just a little more to write.
Related
So I'm extracting a JSON schema out of my RAML spec to validate output in GET methods and also the input in the POST method.
Every entity of this type has a "required" ID property - at least it's required when listing those entities in a 'get item' or 'get collection'-request.
But when validating the received post data to create such an entity the ID is obviously not required (and discarded if it's send anyway).
What's the best DRY way to have this ID property required for GET requests, but not required, or even better not existing in the type for POST requests?
TL;DR: start reading below ;)
Example to make it easier to understand:
For GET requests the type should be like:
properties:
id:
something1:
something2?:
For POST requests the type should be like:
properties:
something1:
something2?:
without having to define both separately, but also without using inheritance to have to create two types for every resource.
Ideally I would solve it that way, but that doesn't seem to work:
get:
description: Retrieve a list of <<resourcePathName|!uppercamelcase>>.
responses:
200:
body:
application/json:
type: [ entity_id_object, <<resourcePathName|!singularize|!uppercamelcase>> ][]
example: <<exampleCollection>>
and entity_id_object ist just:
entity_id_object:
properties:
id:
I think it's because the <<resourcePathName|!singularize|!uppercamelcase>> is not working in this combination.
I can't think of a way of doing it without two types yet. But this example at least only makes you only pass one type and automatically appends 'NoId' to the type name for POST requests.
#%RAML 1.0
title: My API
version: v1
mediaType: application/json
types:
ResponseNoId:
properties:
something1:
something2?:
ResponseId:
properties:
id:
something1:
something2?:
Response:
ResponseNoId|ResponseId
resourceTypes:
collection:
usage: Use this resourceType to represent a collection of items
description: A collection of <<resourcePathName|!uppercamelcase>>
get:
description: |
Get all <<resourcePathName|!uppercamelcase>>,
optionally filtered
is: [ hasResponseCollection: { typeName: <<typeName>> } ]
post:
description: |
Create a new <<resourcePathName|!uppercamelcase|!singularize>>
is: [ hasRequestItem: { typeName: <<typeName>> } ]
item:
usage: Use this resourceType to represent any single item
description: A single <<typeName>>
get:
description: Get a <<typeName>>
is: [ hasResponseItem: { typeName: <<typeName>> } ]
traits:
hasRequestItem:
body:
application/json:
type: <<typeName>>
hasResponseItem:
responses:
200:
body:
application/json:
type: <<typeName>>
hasResponseCollection:
responses:
200:
body:
application/json:
type: <<typeName>>[]
/myResource:
type: { collection: { typeName: Response } }
get:
/{id}:
type: { item: { typeName: Response } }
post:
body:
application/json:
You can mark the id field as readOnly to make the intent clear, although it won't have an effect on the way that the data is validated.
To affect validation, you can create a "Read" type and a "Write" type, where the "Read" type has the extra required id property.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyWriteEntity": {
"type": "object",
"properties": {
"something1": { "type": "string"},
"something2": { "type": "string"}
},
"required": "something1"
},
"MyReadEntity": {
"allOf": [
{ "$ref": "#/definitions/MyWriteEntity" },
{
"id": { "type": "string", "readOnly": true},
"required": ["id"]
}
]
}
}
}
The best way to do this is to create a RAML Library fragment, lets say "foo.raml" for a data model "foo" for resources /foos and /foos/{fooID}.
In the library, create three types:
fooInput:
properties:
something1:
something2:
fooOutput:
type: fooInput
properties:
id: integer
fooPatch:
properties:
something1?:
something2?:
Now in ONE file you have 3 data types. Use the Input version with PUT/POST, use the Output version for GET and use the Patch version for PATCH endpoints. This is even better when paired with resourceTypes.
I am creating API by Swagger 2.0. Everything is good on Swagger 2.0, but, when build it to Slate, all the example value is not shown, just show the data type.
Ex. In swagger, what I want is:
{
"cd": 0,
"data": {
"Num1": "113.90083333",
"Num2": "113.5991"
}
}
But in Slate interface after being built,
{
"cd": "string",
"data": {
"Num1": "number",
"Num2": "number"
}
}
YAML code:
/mar:
get:
tags:
- MP
operationId: "MP1"
produces:
- application/json
responses:
200:
description: "WELL"
schema:
type: object
properties:
cd:
type: string
example: 0
data:
type: object
properties:
Num1:
type: number
example: "113.90083333"
Num2:
type: number
example: "113.5991"
I have been given the attached RAML file to use in Mule but I am having problems working out how to clean up the errors in the file and not even sure this raml file conforms to standards. The errors I am getting are for missing {} and another is missing block entry when I remove the version. Can't figure out how to resolve them.
Below is a cut down version of the RAML:
#%RAML 0.8
---
title: Databox
version: v1
protocols: [HTTPS]
baseUri: https://databox/v1/{version}
mediaType: application/json
traits:
- http-data: !include http-data.raml
resourceTypes: !include types.raml
documentation:
- title: Home
content: |
Databox 1st draft
/stores:
type:
store:
description: Stores
dataSchema: !include stores.json
The traits (http-data.raml):
responses:
200:
description: |
Success
The resourceType (types.raml):
- store:
head:
description: Retrieve data for <<description>>.
is: [ http-data ]
get:
description: Retrieve data for <<description>>.
responses:
200:
body:
application/json:
schema: |
{
"type": "object",
"properties": {
"meta": {
"title": "Data",
"type": "object",
"properties": {
"createdOn": {
"type": "string",
"format": "date-time"
}
},
"required": [
"createdOn"
]
},
"data": {
"type": "array",
"items": <<dataSchema>>
}
},
"required": [
"data"
]
}
description: |
Success. Returns a JSON object containing all <<description>>.
The schema (stores.json):
{
"id": "http://localhost:8000/stores.json#",
"$schema": "http://json-schema.org/draft-04/schema",
"title": "Databox Store Schema",
"type": "object",
"properties": {
"storeId": {
"type": "string"
},
"storeDescription": {
"type": "string"
},
},
"required": [
"storeId"
],
"additionalProperties": false
}
Thanks
RAML is valid except for that <<dataSchema>> parameter used in the json schema, not sure if that's a valid use of parameters.
I would start by replacing that <<dataSchema>> for the json in stores.json and try again.
Let me know if that works or what errors you get.
UPDATE:
Mulesoft's anypoint portal validates your RAML with just that single change, you can see it here
We have following json response for /papers/15
{
"data": [
{
"id": 1,
"title": "foo"
},
{
"id": 2,
"title": "bar"
}
],
"meta": {
"total": 15
}
}
Does anyone know how to describe it swagger yaml file?
Ok, I just figured out how to do this, in case somebody will need id.
Beside dedicated model definitions section ("definitions") it is possible to do inline model descriptions. Code above will looks like:
responses:
"200":
description: Matched Objects
schema:
type: object
properties:
data:
type: object
properties:
authors:
type: array
items:
$ref: "#/definitions/object_with_id_and_title"
Is there a way to have the API Kit Router validate incoming schema? I have the following in my RAML file but it does not validate the incoming schema.
- emails: |
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type" : "object",
"properties" : {
"email" : {
"type" : "string"
},
"name" : {
"type" : "string"
},
"emailOrigin" : {
"type" : "string"
}
}
}
resourceTypes:
- postbase:
post:
responses:
200:
body:
application/json:
500:
body:
application/json:
- putBase:
put:
responses:
200:
body:
application/json:
500:
body:
application/json:
/emails:
type: postbase
post:
description: |
Recieve emails captured from various parts of the site.
body:
schema: emails
The following references help you further
http://forums.raml.org/t/examples-validations-in-raml-parsers/80
Further Example as below: employeeDetailsSchema.json
{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "http://jsonschema.net",
"required": true,
"properties": {
"employeeID": {
"type": "string", -------> Validates the Data type
"required": true -------> Validates whether data is present or not
},
"employeeFirstName": {
"type": "string",
"required": true
},
"employeeLastName": {
"type": "string",
"required": true
},
"employeeDOB": {
"type": "string",
"required": true
}
}
}
Schema file used in my RAML
#%RAML 0.8
title: ManageEmployees
version: 1.0
baseUri: http://api.acme.com/
mediaType: application/json
/newEmployee:
post:
description: Create new employees
body:
schema: !include com/ww/schema/employeeDetailsSchema.json
put:
description: Update employees details
body:
schema: !include com/ww/schema/employeeDetailsSchema.json
responses:
200:
body:
example: !include com/ww/schema/employeeExample.json
As far as I can see, any body will be valid for that schema.
All the fields are string, not required, not any specific format.
Try putting some of the fields as required and see what happens
Cheers!