How to express "arbitrary JSON" in swagger-spec? - json

Let's say I have a REST service that can accept any arbitrary JSON in the request body. How do I model this using swagger-spec?
I thought about Model Objects, but I could only think to wrap the arbitrary JSON (as a string) within a container JSON object, like {"payload": "{ some JSON object serialized to a string }"}, which isn't really useful.
Or, is there some other way to express that an endpoint can receive arbitrary JSON in the request body?

Model the request's body payload as parameters with schema of just "type": "object". The swagger UI editor will then prompt the user with a large textarea containing {} which they can populate with a JSON object.
"/endpoint": {
"post": {
"parameters": [
{
"description": "Arbitrary JSON object payload",
"in": "body",
"name": "body",
"required": true,
"schema": {
"type": "object"
}
}
]
}

Swagger tries to be deterministic when it comes to APIs, so what you're asking is not directly supported.
The only way I can think of to achieve what you want is to set the "consumes" property to "application/json" and add a "body" parameter of type string. This would in theory say that only JSON should be sent, but in effect, any string could be sent.
Also, this may break some third party tools if they'd try to convert to string to a JSON object before sending it to the server.

Related

Swagger - OpenAPI 3.0 : Defining additionalProperties in RequestBody to represent JSON

I have a REST API which accepts application/json in RequestBody. The keys in the json is not predefined. So I have used additionalProperties in swagger to define this JSON. Also, this json may hold JSONArray also.
The problem is, when additionalProperties is used, it is internally taken as Map<String, Object> in the generated java code. This map is deserialized to JSON by using Gson internally. During this conversion, the below json
{
"BooleanField": "true",
"ArrayField": [
"SDK ADD",
"SDK ADD 2"
],
"StringField": "SDK",
"IntField": "1"
}
is converted as
{
"BooleanField": "true",
"ArrayField": {
"myArrayList": [
"SDK ADD",
"SDK ADD 2"
]
},
"StringField": "SDK",
"IntField": "1"
}
The json array is put in myArrayList key. This causes input validation failure for this request. Is there any better way to solve this?

Problem generating Spec for JSON Response type of Array of Users

I've got a Go project, exposing REST CRUD APIs, for a Mongo collection. I'm using go-swagger to generate the swagger spec. However, I'm having trouble getting the JSON response to look like I want without breaking the go-swagger spec generator.
I'm trying to use go-swagger to generate a swagger-spec from annotations on go code. I'd like to see if I can make the response just be a JSON Array of Users, like below.
Is there a way to adjust the json annotation on the User struct to produce the desired result?
[
{"id": "5d8e9aaca00ef6123c989f69", "user_name": "zbeeblebrox"},
{"id": "5d8e9ab1a00ef6123c989f6a", "user_name": "another_user"}
]
Below is what I'm getting, understandably, a JSON object, containing key "data", with a value of an Array of User objects.
I've tried redefining the swagger response struct to be an alias of type []*User, which creates the right response body, but it breaks the go swagger generator.
{
"data": [
{"id": "5d8e9aaca00ef6123c989f69", "user_name": "zbeeblebrox"},
{"id": "5d8e9ab1a00ef6123c989f6a", "user_name": "another_user"}
]
}
Here's some code.
// swagger:model
type User struct {
Id *primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
UserName string `json:"user_name" bson:"user_name"`
}
// HTTP status code 200 and an array of repository models in data
//swagger:response usersResp
type swaggUsersResp struct {
// in:body
Data []*User `json:"data"`
}
I also tried doing it as an alias, which provides the desired JSON response, but breaks Go-Swagger code generation. I suspect this is because the swagger:response annotation is expected to be on a struct, not an alias.
// swagger:response usersResp
type swaggUsersResp = []*User

How to output json in spring mvc

I am using Spring boot with mvc. I have a json schema for request and response. Basically the user posts data (in json) to a url, the controller does its logic and returns a json. I am having difficulty in returning the json. So far, I have a couple of views (in thymeleaf) with hardcoded responses which I dont want. I just want to use an object which I can edit and send back to the client. The response is very simple. This is the schema of the response:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"message":{
"minLength":1,
"type":"string"
}
},
"required": [
"message"
]
}
I have an object of type response which conforms to my response json schema. I basically want to output this object to the client. But not sure how as my controller returns a String and this string is usually the name of the html page.
Add the #ResponseBody annotation to your controller method and it will use the returned value as a response rather than as a path to a page.
If you want that all methods of your controller return direct data (and not page paths), you can annotate your controller with #RestController rather than #Controller.

What is the best practice of testing JSON data?

I'm developing a server-side RESTful application which serves json data for its client applications. And I have to test so many various json outputs.
Each json has many properties and their validation methods are different like below a sample json.
So such my use-case, do you know good libraries or web services to test json data flexibly?
{
"system": { // data structure validation
"time": 1234566, // data type validation
"version": "0.0.1" // string matching validation
},
"app": { // data structure validation
"id": "1234", // string matching validation
"command": "do something", // string matching validation
"data": { // data structure validation
"hoge": "xxx", // data type validation
"fuga": 123 // data type validation
}
}
}
What do you mean by validating? validating the JSON structure or the data inside your JSON object?
To validate the structure you can parse it to another datatype like dictionary and see if you get any error while.
But to validate the data inside the object you need to validate each object in a very specific way for that object.
Use this link Just Place your JSON code and Test it. Quite easy.

JSON for complex and simple data type

Im developing a WCF service that accepts JSON. My method signature accepts 2 parameters, a complex object and a simple type. For all intents and purposes below, assume "servicecredentials" has 2 properties, "username" and "password". I have valid JSON, but when I use a tool like postman I get the error "Expected to find an attribute with name 'type' and value 'object'. Found value 'array'.'"
How should this JSON be posted to the method?
<OperationContract()>
<WebInvoke(method:="POST")>
Function GetStuff(ByVal creds As servicecredentials, ByVal acctNum As String)
The JSON Im posting
[
{
"UserName": "someUSer",
"Password": "p#ssw0Rd"
},
{
"acctNum": "X12362"
}
]
The [] brackets indicate a JSON Array, the {} brackets indicate a JSON Object. If you encompass the array with the {} brackets it will be an object, which is what it seems to be looking for.
Example:
{
"data": [
{
"UserName": "someUSer",
"Password": "p#ssw0Rd"
},
{
"acctNum": "X12362"
}
]
}
The exact internal structure of the JSON depends on how the method will process the data. The error is simply stating that the JSON is not encompassed by an object.