How to define schema arbitrary JSON object? - json

I am trying to define a API using OAS v2 that will return a payload along with some metadata.
In other words, the response to the API will be:
{
"metadata":[
{
"key" :"key1",
"value": "value1"
}
],
"payload": {Valid JSON}
}
The payload can be anything, and different for different scenarios, the only constraint being it will be a valid JSON format. So, at this point in time, I would like to define it just as a JSON object without defining the field level details.
How can I do that in OAS2/JSON schema?
Thanks in advance.

In this case you can use an empty schema ({}) for payload.
JSON Schema relies on valid JSON value and you won't be able to supply it if your response is a malformed JSON.
If payload value is malformed, whole response will not be a valid JSON.
In other words, this issue is out of JSON Schema scope, but rather in scope of your response decoder (that should fail on malformed response body).

Related

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

Why doesn't the json model's odata property have columns?

I am creating a JSON model in UI5 from a json object. But it is missing the columns array in the JSON model. I checked the model in the console and the odata property had no columns. Check image for proof.
https://ibb.co/hLmYpZb
Hence I want to know how I can get the column to show up.
Here is my code.
var emptyModel = {
"columns": []
};
this.setModel(new JSONModel(JSON.stringify(emptyModel)), "selcontent");
I expect column to show up in the JSON model under the odata property.
JSONModel awaits a plain object or a URL string as the first argument.
Either the URL where to load the JSON from or a JS object (source)
The result of the stringified emptyModel is "{"columns":[]}" which is neither an object nor a URL.

How to convert a String array to Swiftyjson JSON type

I need to pass an array of string/Int (doesnt matter) as JSON parameter in a HTTP body for a POST request.
{
"par1" : value,
"par2" : "value2",
"par3" : ['123:456', '123:234' ...]*
}
*My problem is for filling my JSON object with param3 values. In the swift code I have them as an array of Strings.
There are many example of converting a JSON object back to an array of strings/Int, but I can't find it the other way around.
As mentioned in the comments, the way to go is to use NSJSONSerialization to generate object encoded in JSON data.

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.

Ember insert underscore in json-serialization

i have the following JSON-GET response:
{
"check_lists": [
{
"id": 2,
"name": "Servicebesuch",
"description": ""
}]
}
Problem is the "check_lists" name which i have solved in one of the extract/extractSingle methods of the RESTSerializer so the data is loaded and deserialized successfully to the model named App.Checklist.
Now i got stucked again.
For serialization of my model-data to json, i have to do the same trick reverse but i can not find a hook for that. The serialize Method of the RESTSerializer gets only the pure record and the serialized json, received from this._super... call, does only hold the serialized object and not the call itself.
The server expects a PUT request (in case of updating a list) with the parameter packed in a param named check_list but ember sends it in a param named checklist.
Processing by CheckListsController#update as JSON
Parameters: {"checklist"=>{"name"=>"Kaffeevertrieb", "description"=>""},
"id"=>"3", "check_list"=>{}}
ActionController::ParameterMissing (param is missing or the value is empty: check_list):
Does someone know how to insert the underscore to the json request?
Greetings