Apiary blueprint markdown for an array response body - jsonschema

I have a REST service that returns an array of Strings (i.e. not an object). The relevant Blueprint markdown from Apiary is as follows:
+ Model (application/json)
+ Body
[
"element1",
"element2",
"element3"
]
+ Schema
{
"type": "array",
"items": {
"type": "string"
}
}
When I run the mock service in Apiary it works, but I get the following error in apiary traffic inspector:
This API request was compared to a documented resource but was found invalid.
Check your request headers and body below.
...
JSON schema is not valid! invalid type: object (expected [object Object]/array)
at path "/items"
Is there a trick to get this working (a change to the markdown), or is this a bug with Apiary?

Apiary support JSON Schema draft 3, this using JSON schema generator - http://www.jsonschema.net/
{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "http://jsonschema.net",
"required":false,
"properties":{
"0": {
"type":"string",
"id": "http://jsonschema.net/0",
"required":false
},
"1": {
"type":"string",
"id": "http://jsonschema.net/1",
"required":false
},
"2": {
"type":"string",
"id": "http://jsonschema.net/2",
"required":false
}
}
}

Related

Facebook ads custom audience Data is missing or does not match schema error

i was building a integration with the facebook ads audience API, and according the documentation the request must be created like this:
POST - https://graph.facebook.com/v15.0/<MY_CUSTOM_AUDIENCE_ID>/users?access_token=<MY_ACCESS_TOKEN>
{
"session":{
"session_id":1,
"batch_seq":1,
"last_batch_flag":true,
"estimated_num_total":1
},
"payload":{
"schema":[
"FN"
],
"data":
[
"8b1ebea129cee0d2ca86be6706cd2dfcf79aaaea259fd0c311bdbf2a192be148"
]
}
}
Using the previus example a received a error 400:
{
"error": {
"message": "(#100) Data is missing or does not match schema",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "AqrLd9uIw0D4BBFtHF33bdU"
}
}
For do this i used this documentation https://developers.facebook.com/docs/marketing-api/audiences/guides/custom-audiences#hash
Anyone has use this before?
Your schema field type is array but array use form multi-key qualification.
Change it to string: schema: 'FN'
In docs you can see all formats.
This payload with multi keys work for me:
{
"session": {
"session_id": 123,
"batch_seq": 1,
"last_batch_flag": true
},
"payload": {
"schema": [
"EMAIL",
"PHONE",
"FN"
],
"data": [
["EMAIL_HASH", "PHONE_HASH", "FN_HASH"]
]
}
}

whatsapp api error during sending an created template

{ "messaging_product": "whatsapp",
"to": "91********5",
"type": "template",
"template": {
"name": "demo_template",
"language": {
"code": "en_US"
}
}
}
This is passed as a post method.
demo_template is an existing template created by me.
Getting error like this:
{
"error": {
"message": "(#132012) Parameter format does not match format in the created template",
"type": "OAuthException",
"code": 132012,
"error_data": {
"messaging_product": "whatsapp",
"details": "header: Format mismatch, expected IMAGE, received UNKNOWN"
},
"error_subcode": 2494073,
"fbtrace_id": "ARtWScjGa0rADjfHvbOH4bS"
}
}
For image you have to first upload the image in media library then you can get id for the media you have uploded then you can attach that id with the messaging template you send out in headers in your case it is image.check this link here
or you can directly use url link to attach media.{ "type": "header", "parameters": [ { "type": "image", "image": { "link": "https://URL" } } ] },
The parameter values included in the request are not using the format specified in the template. See the Message Template Guidelines.

API standard design - swagger parameter body VS URI

I would like to clarify what is the design standard to query a list of array.
json data:
"ids": [
{
"id": "bbbbeeee1111",
"type": "student"
},
{
"id": "bbbbeeee2222",
"type": "teacher"
}
],
GET endpoint
/api/testitem
swagger parameter body
{
"ids": ["bbbbeeee1111;student"]
}
swagger returned the URI like this , the %3B seems wrong. some coding issues at API design?
http://localhost:8888/api/testitem?ids=bbbbeeee1111%3Bstudent

How to setup Swashbuckle/swagger-ui to render an array of strings as comma-delimited and correctly quoted within a multipart/form-data request?

I am using ASP.NET Core 3.0 (<TargetFramework>netcoreapp3.0</TargetFramework>) and Swashbuckle 5.0.0-rc4 (<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />).
I have an API Controller Action Method which accepts an uploaded file (that is, an IFormFile) and some metadata and tags to associate with the POSTed file:
public async Task<IActionResult> Post(
string fileId = null,
IFormFile file = null,
[FromForm] IDictionary<string, object> metadata = null,
[FromForm] IEnumerable<string> tags = null)
{
// ...
}
I am able -- through sheer luck -- to get Swashbuckle/swagger-ui to send the metadata in a format that seems to be correct (except, I need to write something that will parse out the JSON from within the multipart/form-data fragment). The metadata parameter is OK.
I am having real problems with the tags parameter. The OpenAPI document generated by Swashbuckle is like this:
"/projects/{projectId}/features/imports": {
"post": {
"tags": [
"FeaturesImports"
],
"summary": "Import features from a file uploaded by the end user, or from a file already stored in XXX",
"parameters": [
{
"name": "fileId",
"in": "query",
"description": "The XXX File (Version) Id of the source file to import. Optional; if file is not provided, a fileIdmust be provided.",
"schema": {
"type": "string"
}
},
{
"name": "projectId",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
},
"metadata": {
"type": "object",
"additionalProperties": {
"type": "object",
"additionalProperties": false
}
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"encoding": {
"file": {
"style": "form"
},
"metadata": {
"style": "form"
},
"tags": {
"style": "form"
}
}
}
}
},
To my eyes, the fact that we have:
"tags": {
"type": "array",
"items": {
"type": "string"
}
seems correct: I want to receive an array of strings.
However, when the swagger-ui renders the form, and the end user enters the required data, the submitted request is not as I would would expect. I would expect the tags data to be something like:
------WebKitFormBoundaryKQWA8MEJTEXgMvsj
Content-Disposition: form-data; name="metadata"
{
"additionalProp1": {},
"additionalProp2": {},
"additionalProp3": {}
}
------WebKitFormBoundaryKQWA8MEJTEXgMvsj
Content-Disposition: form-data; name="tags"
["string1","string2",",","string4\""]
------WebKitFormBoundaryKQWA8MEJTEXgMvsj--
but what I end up getting is:
------WebKitFormBoundaryKQWA8MEJTEXgMvsj
Content-Disposition: form-data; name="metadata"
{
"additionalProp1": {},
"additionalProp2": {},
"additionalProp3": {}
}
------WebKitFormBoundaryKQWA8MEJTEXgMvsj
Content-Disposition: form-data; name="tags"
string1,string2,,string4"
------WebKitFormBoundaryKQWA8MEJTEXgMvsj--
The problems with this are:
the when the data is bound to the Action Method's parameters, the IEnumerable<string> tags parameter only contains one string, and not the 4 I would expect
the individual strings are joined, using a comma; and any commas present in the "raw" strings are not escaped. It is therefore impossible to reconstruct the original intended data
Given the metadata parameter appears to be JSON encoded, I would expect the same for the tags parameter. However, the OpenAPI 3.0.2 spec says (at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#special-considerations-for-multipart-content) that,
If the property is complex, or an array of complex values, the default Content-Type is application/json
... so that explains the JSON encoding of the metadata parameter, and...
If the property is a primitive, or an array of primitive values, the default Content-Type is text/plain
... ah, and that explains the encoding of the tags parameter, since it is an array of primitive values.
I think the underlying problem is that swagger-ui does not yet support the "Encoding Object" that can be set for multipart parameters, but does anyone have any suggestions, tips, workarounds or other ideas that might be helpful here?

Unable to resolve raml file errors

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