Swagger editor always gives error on json - json

For me not a single json string worked with the swagger editor.
Here is an example which i took from the page:
{
"swaggerVersion": "2.0",
"basePath": "http://localhost:8000/greetings",
"apis": [
{
"path": "/hello/{subject}",
"operations": [
{
"method": "GET",
"summary": "Greet our subject with hello!",
"type": "string",
"nickname": "helloSubject",
"parameters": [
{
"name": "subject",
"description": "The subject to be greeted.",
"required": true,
"type": "string",
"paramType": "path"
}
]
}
]
}
],
"models": {}
}
So current error for this, is:
✖ YAML Syntax Error
Missed comma between flow collection entries at line 2, column 14: "swagger: "2.0", ^

I don't know where did you take the base code for but that syntax seems to be totally wrong (at least for swagger 2), I recommend you to take a look to the official specification.
About the code you pasted, I refactorized it, try with this one:
{
"swagger": "2.0",
"host": "localhost:8000",
"basePath": "/greetings",
"info": {
"title": "Some title",
"version": "0.0.1"
},
"paths": {
"/hello/{subject}": {
"get": {
"summary": "Greet our subject with hello!",
"parameters": [{
"name": "subject",
"description": "The subject to be greeted.",
"required": true,
"type": "string",
"in": "path"
}],
"responses": {
"default": {
"description": "Some description",
"schema": {
"type": "string"
}
}
}
}
}
}
}

Even though the OP's syntax was wrong, this is for other people who are searching for an answer and their syntax is correct.
If you are getting the JSON data from the browser and you have a browser extension that formats and displays nicely the JSON data, it might cause trouble (because of the formatting) when pasting in Swagger Editor.
If that's the case, try disabling the extension or go incognito.

Related

Use JSON schema check on POST request with WireMock

I'm using WireMock (Docker) docker.io/rodolpheche/wiremock:2.27.2-alpine. I set it up via Docker Compose and everything works flawlessly.
I'm trying to set up a stub/mapping to check the JSON input against the corresponding JSON schema. Basically, this is the stub/mapping:
{
"name": "Create Character Stub",
"request": {
"headers": {
"accept": {
"caseInsensitive": true,
"equalTo": "application/json"
},
"content-type": {
"caseInsensitive": true,
"equalTo": "application/json"
}
},
"method": "POST",
"url": "/api/characters",
"body": {
// [start] I made this up, but that's what I'm trying to achieve
"type": "JSON_SCHEMA",
"location": ""schemas/create-character.json""
// [end]
}
},
"response": {
"headers": {
"location": "{{ request.path }}/{{ randomValue type='UUID' }}"
},
"status": 201,
"transformers": ["response-template"]
}
}
...and this is (an excerpt of) the JSON schema:
{
"$schema": "http://json-schema.org/draft/2019-09/schema#",
"type": "object",
"description": "Character entity",
"properties": {
"first_name": {
"type": "string",
"description": ""
},
"last_name": {
"type": "string",
"description": ""
},
"age": {
"type": "number",
"description": ""
}
},
"additionalProperties": false,
"required": ["first_name", "last_name", "age"]
}
Is there a way to accomplish this with WireMock? I've been searching for something like that for a while. The closer I've found is to use JSON path, but that's not quite the same as checking for a proper schema — and could be tedious task with long JSON requests.
I searched through the issues in the WireMock's GitHub repository, but I can only see this one (closed by the way). Still, I don't see a way to do exactly what I'm looking for. I just have as input a JSON schema.

How to merge swagger multiple files in single swagger doc file?

I have different swagger files for multiple APIs, like swagger1.json for OpenStack API, swagger2.json for Users API etc and I was trying to merge these all swagger files in one single file using remote $ref method.
Here is swagger1.json file for OpenStack api
{
"stacks": {
"get": {
"tags": [
"openstack"
],
"summary": "Returns stacks from OpenStack",
"description": "Returns all stacks from the OpenStack based on tenantId.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "query",
"type": "string",
"name": "tenantId",
"description": "search stacks for tenantId from OpenStack",
"required": false
}
],
"responses": {
"200": {
"description": "OK"
},
"400": {
"description": "Unknown Error"
},
"401": {
"description": "Unauthorized"
}
}
}
}
}
And this is the swagger-merge.json where I want to add multiple swagger doc file using remote reference.
{
"swagger": "2.0",
"info": {
"description": "something here",
"version": "v0.7.0",
"title": "The API Gateway",
"contact": {
"email": "dp#gmail.com"
}
},
"host": "localhost",
"port":"9191",
"basePath": "/api/openstack",
"tags": [
{
"name": "OpenStackApi",
"description": "Get stacks and running instance form OpenStack"
}
],
"schemes": [
"https"
],
"paths": {
"$ref": "./swagger1.json#/stacks"
}
}
This isn't working for me. I am not able to see API methods I have written inside swagger1.json file. I have upload swaggerUI output. Any idea about what I am doing wrong and how can I solve this issue?
You can't $ref the whole contents of paths, you can only refer individual path items:
"paths": {
"/stacks": { <--- endpoint path
"$ref": "./swagger1.json#/stacks"
}
}

Problems with v1/queryContext needed for SpagoBI integration

In version 1.7.0 of Orion CB running the docker version in Docker for Windows,
if I create a simple object doing POST http://localhost:1026/v1/updateContext
with the body:
{
"contextElements": [
{
"type": "Car",
"id": "myNewCar",
"attributes": [
{
"name": "maxSpeed",
"type": "integer",
"value": "220"
}
]
}
],
"updateAction": "APPEND"
}
I get the answer:
{
"contextResponses": [
{
"contextElement": {
"type": "Car",
"isPattern": "false",
"id": "myNewCar",
"attributes": [
{
"name": "maxSpeed",
"type": "integer",
"value": ""
}
]
},
"statusCode": {
"code": "200",
"reasonPhrase": "OK"
}
}
]
}
Then, if I do POST http://localhost:1026/v1/queryContext with the same headers and the same components with the body
{
"entities": [
{
"type": "Car",
"isPattern": "false",
"id": "myNewCar"
}
]
}
I get the following:
{
"errorCode": {
"code": "404",
"reasonPhrase": "No context element found"
}
}
Which shouldn't be problematic (I can query the entities with v2 API, for instance) if it wasn't needed for integration with data representation tools such as SpagoBI as documented in http://spagobi.readthedocs.io/en/latest/user/NGSI/README/
What can I do? I am doing something wrong with the context provision?
Thanks!
My problem was that I was using a imported Postman collection of the API (Downloaded from https://github.com/telefonicaid/fiware-orion/tree/develop/doc/apiary/v2) and accidentally I was using the header Fiware-Service.
You are right and your tests work properly.
Thanks for the prompt reply!!

JSON schema validation for base64 string is not working

I am using following schema to validate image base64 string:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"image": {
"type": "string",
"media": {
"contentEncoding": "base64",
"oneOf": [
{ "mediaType": "image/png" },
{ "mediaType": "image/jpg" },
{ "mediaType": "image/jpeg" }
]
}
}
},
"required": [
"image"
]
}
But the schema is not working, any string, even invalid, is passed to application
I am using JSON Schema for PHP to validate json input
UPDATE
This what I tried to change, but it doesn't work
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"image": {
"title": "image",
"type": "string",
"media": {
"binaryEncoding": "base64",
"type": "image/png"
}
}
},
"required": [
"image"
]
}
Your problem is old syntax which is no longer supported in JSON Hyper-Schema Draft #4. It changed contentEncoding into binaryEncoding (see the change log here).
A relevant excerpt from the spec follows:
"imgData": {
"title": "Article Illustration (small)",
"type": "string",
"media": {
"binaryEncoding": "base64",
"type": "image/png"
}
}
Also, you mentioned nothing about which validator you're using. It may prove important, as compliance across different implementations may vary.
Update: Also, the $schema should be http://json-schema.org/draftv4/hyper-schema for hyper-schema processing.

Orion JSON Bad Request

I'm currently trying to subscribe Orion and Cosmos. All data sent to Orion is being updated without any issue. But, when posting to http://xxx.xxx.xx.xx:1026/v1/subscribeContext I'm getting the following error:
{
"subscribeError": {
"errorCode": {
"code": "400",
"reasonPhrase": "Bad Request",
"details": "JSON Parse Error"
}
}
}
This is the json string I'm sending:
{
"entities": [
{
"type": "Location",
"isPattern": "false",
"id": "Device-1"
}
],
"reference": "http://52.31.144.170:5050/notify",
"duration": "PT10S",
"notifyConditions": [
{
"type": "ONCHANGE",
"condValues": [
"position"
]
}
],
"attributes": [
"position"
]
}
The entity updating OK in Orion is:
{
"type": "Location",
"isPattern": "false",
"id": "Device-1",
"attributes": [
{
"name": "position",
"type": "coords",
"value": "24,21",
"metadatas": [
{
"name": "location",
"type": "string",
"value": "WGS84"
}
]
},
{
"name": "id",
"type": "device",
"value": "1"
}
]
}
I've tried with many different examples from readthedocs, and other responses in StackOverflow unsuccessfully.
Which is the correct format? Should I call /subscribeContext before or after updating Orion with /contextEntities?
Orion Context Broker version is 0.26.1.
Thank you in advance.
Taking into account that the same payload works ok when send using curl (see this execution session) I tend to think that some issue in the client (maybe hiden by a programming framework?) is causing the problem.