Referencing enum shows 'Hello, world!' in body - apiblueprint

I'm using api-blueprint.
Some requests have checkbox on/off values so that I want to declare some data-structure to describe checkbox status.
FORMAT: 1A9
# API
## Register [POST /api/v1/register]
+ Request (application/json)
+ Attributes(CreateUserRequest)
+ Response 201 (application/json)
# Data Structures
## CreateUserRequest (object)
+ `accept_terms`: off (CheckboxSelection, required)
## CheckboxSelection (enum[string])
### Items
- on (string)
- off (string)
### Default
- off
This apib generates body below
{
"accept_terms": "Hello, world!"
}
Schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"accept_terms": {
"type": "string",
"enum": [
"on",
"off",
"off"
],
"default": "off"
}
},
"required": [
"accept_terms"
]
}
Why value in body shows 'Hello, world!'Am I doing something odd?
Is it better to write enum in Request data-structure?
FORMAT: 1A9
# API
## Register [POST /api/v1/register]
+ Request (application/json)
+ Attributes(CreateUserRequest)
+ Response 201 (application/json)
# Data Structures
## CreateUserRequest (object)
+ `accept_terms`: on, off (enum[string], required)
- Default: off
This generates body
{
"accept_terms": "on"
}
and the schema is equivalent to first one.

Related

How to handle JSON array response while using APIConsumer?

Using the APIConsumer contract we can feed data from API to the smart contract.
Eg: If the server response is:
{
"RAW":{"ETH":{"USD":{"VOLUME24HOUR": 10000,}}}
}
Then, data can be obtained as:
request.add("get", URL);
request.add("path", "RAW.ETH.USD.VOLUME24HOUR");
Similarly, If the server response contains some JSON array,
Eg:
{
"date":"530934083405834",
"results": [
{
"id": 9865,
"rank":1
},
{
"id": 9869,
"rank": 2
},
{
"id": 9866,
"rank": 3
}
]}
Then in this case is there a way to get the id of the rank 1 i.e results[0]["id"]?
To get results[0]["id"] your path in the request needs to be
request.add("path", "results.0.id");

How to iterate through the contents of a Json object

On the JSR223 assertion in Jmeter, I need to validate only the inner part of the JSON returned.
I followed this thread to get an idea on the validation.
​How can I write JSON schema validation for JMeter run in TeamCity
Basically my Jmeter sampler returns the json as follows. On my schema, the validation should be for items, service and requestId. No validation should be performed for "payload".
{
"payload": [
{
"items": [
{
"code": "487482378",
"description": "Alpha Co",
"valid": true
},
{
"code": "92901128365",
"description": "Beta Co",
"valid": true
}
],
"service": "entities",
"requestId": "d190219"
}
]
}
This is my current code in the js223 sampler:
var schemaPath = '/path/entities-schema.json'
var rawSchema = new org.json.JSONObject(new org.json.JSONTokener(org.apache.commons.io.FileUtils.readFileToString(new java.io.File(schemaPath), 'UTF-8')))
var schema = org.everit.json.schema.loader.SchemaLoader.load(rawSchema)
schema.validate(new org.json.JSONObject(prev.getResponseDataAsString()))
You can remove the "unwanted" part of the response using JSR223 PostProcessor like:
def before = prev.getResponseDataAsString()
log.info('Before: ' + before)
def response = new groovy.json.JsonSlurper().parseText(before)
def after = new groovy.json.JsonBuilder(response.payload.items).toPrettyString()
log.info('After: ' + after)
prev.setResponseData(after, 'UTF-8')
Once done you can use your JSON Schema validation approach against the new content without elements you don't need.
References:
Groovy: Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

laravel/codeception : test if json response contains only certain keys

I have a json array coming from my api as response:
{
"data": [
{
"id": 1,
"name": "abc"
}
}
I am using laravel for api and laravel-codeception for testing.
public function getAll(ApiTester $I)
{
$I->sendGET($this->endpoint);
}
I have to test if the response contains only id and name key (not any other key) example this response should fail the test.
{
"data": [
{
"id": 1,
"name": "abc",
"email":"abc#xyz"
}
}
I have found $I->seeResponseContainsJson(), but it checks if JSON is present or not. It does not check if JSON response contains only specified keys.
Thanks.

Wrapping Data Structure in a data key API Blueprint / Apiary

So let's say I have a 200 response which body should be:
{
"data": [
{
"id": 1,
"title": "Activity 1"
},
{
"id": 1,
"title": "Activity 2"
}
]
}
I have managed to get this behavior of the response body by using this in API Blueprint.
+ Response 200 (application/json)
+ Attributes
+ data (array[Activity])
(Note that I can't add the data key to the data structure itself, because it's only present on the single response. If I need to nest an Activity inside another structure, it shouldn't have the data key.)
This doesn't seem right
The reason why I don't think it's the correct way of doing it, is beacuse of the JSON schema for this response which is:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"data": {
"type": "array"
}
}
}
Note how the actual activity is excluded.
How can I wrap my response in a data key properly, and have it reflected in both the body AND the schema?
You should use this line:
+ data(array[Activity], fixed-type)
The fixed-type keyword fixes the type of the items in the array.

Why doesn't the Apiary Console give a correct response for this API Blueprint?

I'm trying to create a simple demo of how API Blueprint works with Apiary.io. For the demo, I wanted to put an endpoint from the Mashape Weather API into blueprint.
Here's the attempt:
http://docs.weatherapi3.apiary.io/#reference/weather/weather-data/get-the-weather-data?console=1
It seems to work (meaning, the Try it out button in the Console gives a response), but I get this result:
{
"query": {
"count": 0,
"created": "2015-06-21T11:12:06Z",
"lang": "en-US",
"results": null
}
}
The same result passed via cURL does give a correct response, which is an output of the weather.
Is there something I am misconfiguring in the blueprint?
Alternatively, does Mashape block calls from Apiary.io?
Here's the blueprint:
FORMAT: 1A
HOST: https://simple-weather.p.mashape.com
# Weather API
Display Weather forecast data by latitude and longitude. Get raw weather data OR simple label description of weather forecast of some places.
# Weather API Root [/]
# Group Weather
Resources related to weather in the API.
## Weather data [/weatherdata{?lat}{?lng}]
### Get the weather data [GET]
Get the weather data in your area.
+ Parameters
+ lat: 55.749792 (required, number) - Latitude
+ lng: 37.632495 (required, number) - Longitude
+ Request JSON Message
+ Headers
X-Mashape-Authorization: {hidden in this post}
Accept: text/plain
+ Response 200 (application/json)
+ Body
[
{
"query": {
"count": 1,
"created": "2014-05-03T03:57:53Z",
"lang": "en-US",
"results": {
"channel": {
"title": "Yahoo! Weather - Tebrau, MY",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Tebrau__MY/*http://weather.yahoo.com/forecast/MYXX0004_c.html",
"description": "Yahoo! Weather for Tebrau, MY",
"language": "en-us",
"lastBuildDate": "Sat, 03 May 2014 11:00 am MYT",
"ttl": "60",
"location": {
"city": "Tebrau",
"country": "Malaysia",
"region": ""
},
...//truncated for this post
}
}
}
}
}
]
There is a tiny mistake in Blueprint in GET parameters. Parameters should be written like: {?param1,param2} (See: URI Templates)
So if you just change
## Weather data [/weatherdata{?lat}{?lng}] to ## Weather data [/weatherdata{?lat,lng}] it works.

Resources