additionalProperties eliminates all model properties - java

I have run into this issue now with implementing arbitrary field behaviour with additionalProperties. Below is my model with additional properties.
"CObject": {
"type": "object",
"properties": {
"_id": {
"type": "string"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"updatedAt": {
"type": "string",
"format": "date-time"
}
},
"additionalProperties": {
"type": "string"
}
}
Indeed the only change I see in the generated java client is that my CObject.java now sub-classes HashMap class. I expected that a call to put([key],[value]) on CObject would just add a new property in the request body. On running a test, I realized that all properties within CObject are absent in the final request body. So, the client only sends a request body with properties defined arbitrarily and eliminates all other properties defined in the specification. Am using the latest swagger-codegen(2.1.6 snapshot). how can I get past this issue???
NB I don't want the map values to appear under their own property, I want the key value pairs to appear under the same hierarchy as the rest of the properties defined in the spec.

In the JSON you provided, "additionalProperties" is just a property name and its type is string. Let's say the property name is "keyValuePair", then the correct way to use additionalProperties is
"keyValuePair": {
{
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}

Related

Plugin jsonschema2pojo: Properties are shown as required but they should be optional

i have a personschema which has e.g. those two fields:
"name": {
"title": "Last name",
"type": "string",
"minLength": 1,
"isRequired" : true
}
"birthday": {
"title": "Date of birth",
"$ref": "resource:schema/general/dateSchema.json",
"isRequired" : true
},
"birthCountry": {
"$ref": "resource:schema/general/countrySchema.json",
"title": "Country of birth",
"isRequired" : true
}
Then I have another schema which extends the person schema. In this schema, I want those two properties (birthCountry and birthday) to be optional and only the name to be mandatory. I've tried it like that:
{
"$schema": "http://json-schema.org/draft/2019-09/schema",
"title": "Other Schema",
"type": "object",
"javaType": "..json_schema_model.dto.OtherSchema",
"description": "Other Schema Description",
"extendsJavaClass": "..json_schema_model.dto.PersonSchema",
"allOf": [{
"required": [
"name"
],
"$ref": "resource:schema/general/personSchema.json"
}
But unfortunately, in the API-Docs they are still remarked as mandatory.
The issue was that properties can only be added in child classes and not modified, so I rearranged the class structure to modify the decision whether a value is mandatory will only be decided in the child classes themselves. Also please note, that sibling values alongsid $refs are ignored. To add properties to a $ref, you need to wrap it into an allOf.

How to include validations for key as well as value in a json schema

I need to add validations for a property as well as a value in json schema.
I tried to use the below schema but none of the validations work :
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"additionalProperties": false,
"minProperties": 1,
"properties": {
"add": {
"type": "object",
"patternProperties": {
"^VOF979[0-9]{11}-NDG[0-9]{2}$": {
"description": "Some description",
"type": "string",
"maxLength": 2
}
}
}
}
}
I used below json data and all the validations passes although the key and value both are wrong :
{
"add": {"VOF98999990005235-NDG01": "121"}
}
JSON Schema is constraints based.
patternProperties applies its value schema to the instance location based on the key match (in this case, regex match).
It does not prohibit additional keys in the object.
If you want to prevent additional keys, you need to specify so.
To do this, you need "additionalProperties": false.
Do not allow additional properties to keep strict validation
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {
"add": {
"type": "object",
"patternProperties": {
"^VOF979[0-9]{11}-NDG[0-9]{2}$": {
"description": "Some description",
"type": "string",
"maxLength": 2
}
},
"additionalProperties": false // This One
}
},
"additionalProperties": false,
"minProperties": 1
}
Reference to Docs Have a look at this

How to validate a field to have specific value only in AVRO Schema

I am using Avro Schema to validate my schema is:
{
"name": "Example",
"type": "record",
"fields": [
{
"name": "custId",
"type": "string"
},
{
"name": "danger",
"type": {
"type": "enum",
"symbols": [0,1,2,3],
"name": "danger"
}}
]
}
i have a requirement where i want field "danger" to have values 0,1,2,3 . If something else is given schema should not validate it. I know enum type are used for that but it allow only string.
How can i achieve this.

Is there a way to dynamically set variables values for the post request with jmeter beanshell preprocessor

I need to dynamically build the following post request JSON body with jmeter beanshell preprocessor. I am referring to the following question which has a solution for my problem with looped strings. I would need to do this with json-property(variables) an array of JSON objects with different name and values. Thanks a lot.
{
"processDefinitionId":"optaplannerkey:1:dbc4af8f-7e04-11e9-afa3-1ecac26bb5e0",
"businessKey":"optaplannerkey",
"returnVariables":true,
"variables": [
{
"name": "TaskDescription",
"value": "Fixing the issue with sink"
},
{
"name": "TaskCategory",
"value": "plumbing"
},
{
"name": "Priority",
"value": "Medium"
},
{
"name": "Status",
"value": "New"
},
{
"name": "SkillsRequired",
"value": "Plumbing Skills"
},
{
"name": "DueDate",
"value": "2019-05-24T11:23:08.030+05:30"
}
]
}
Use dummy sampler with the parameterized json request and CSV Data Set Config for the dynamic input. Below, I have paremeterized only two for demo.
Then, Use JSR223 Post processor with the following code:-
vars.put("responseVar",prev.getResponseDataAsString());
This will put response body in "responseVar" variable. Fetch it using ${responseVar}
Hope this helps.

declare java.util.Map into json-schema

I need to map a java.util.Map instance into a JSON-schema that is used by org.jsonschema2pojo maven plugin to create a POJO.
I didn't find a good and simple solution for this.
Could someone help me please?
This is my actual json-schema file
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Response",
"description": "A Response object",
"type": "object",
"properties": {
"result": {
"type": "string",
"description": "describes response status"
},
"msg": {
"type": "string",
"description": "user msgs"
}
},
"required": ["result"],
"additionalProperties": false
}
I need to add a field "errors" that is converted into a java.util.Map<String, String> in Java.
AFAIK additionalProperties does the job.
You can declare an errors property of type Map<String, Object> for example like this (this is yaml now):
...
properties:
errors:
type: object
additionalProperties:
type: object
You don't specify the type of the keys, since this describes a json document, which naturally has strings as keys on objects.
instead of type: object you can also do type: string for Map<String, String> or reference another definition if you have your own type as values in that map.

Categories

Resources