How to set multiple values as defaultValue using Springfox 2.7.0, 2.9.2 - springfox

I have a request parameter that accepts an array of string/enums, however when I validate the docs with openapi-generator, it says that the default is not of type 'array'. Looking at the api-docs, I see it made the default value a string, rather than an array.
How do I annotate the Springfox swagger so that I can list multiple values as the default selection?
#GetMapping
public PagedResults<PortfolioDto> getVisiblePortfolios(
#ApiParam("List of Visibility")
#RequestParam(defaultValue = "COMPANY,GLOBAL") List<PublicVisibility> visibility) { }
produces
{
"name": "visibility",
"in": "query",
"description": "List of Visibility",
"required": false,
"type": "array",
"items": {
"type": "string",
"enum": [
"COMPANY",
"GLOBAL"
]
},
"collectionFormat": "multi",
"default": "[COMPANY,GLOBAL]",
"enum": [
"COMPANY",
"GLOBAL"
]
}
and the error
-attribute paths.'/api/discover/portfolio'(get).[visibility].default is not of type `array`

Related

Django defining a json type on model makes jsonb instead of json

I am using the JSONField for my Django model for JSON type but in the background, it makes my attributes column type JSONB which I don't want to do here because it breaks my exact order of JSON'S internal field that I pushed from my frontend app. You can see the order of fields is not my exact value. To store it as it is, I need to use JSON instead of jsonb. So my question is how to do that?
What I pushed:
{
"type": "array",
"title": "action_right",
"additionalProperties": true,
"items": {
"type": "object",
"required": [
"label",
"url"
],
"properties": {
"label": {
"type": "string",
"custom_type": "string",
"title": "label",
"default": ""
},
"url": {
"type": "string",
"custom_type": "string",
"title": "url",
"default": ""
}
}
}
}
What is stored:
{
"type": "array",
"items": {
"type": "object",
"required": [
"label",
"url"
],
"properties": {
"url": {
"type": "string",
"title": "url",
"default": "",
"custom_type": "string"
},
"label": {
"type": "string",
"title": "label",
"default": "",
"custom_type": "string"
}
}
},
"title": "action_right",
"additionalProperties": true
}
Code snippets:
class Component(models.Model):
name = models.CharField(max_length=200, unique=True)
attributes = models.JSONField(default=dict, help_text='Component attributes in JSONSchema')
Notes:
PostgreSQL has two native JSON based data types: json and jsonb. The
main difference between them is how they are stored and how they can
be queried. PostgreSQL’s json field is stored as the original string
representation of the JSON and must be decoded on the fly when queried
based on keys. The jsonb field is stored based on the actual structure
of the JSON which allows indexing. The trade-off is a small additional
cost on writing to the jsonb field. JSONField uses jsonb.
Try this (may need to be tweaked):
import json
class RawJSONField(models.JSONField):
def db_type(self, connection):
return 'text'
def get_prep_value(self, value):
return json.dumps(value)
def from_db_value(self, value, expression, connection):
if value is None:
return value
return json.loads(value)
in some model:
class Calculation(models.Model):
calc_result = RawJSONField(_("Calc result data"), default = dict)
other variant:
from django.db import models
import json
class RawJSONField(models.TextField):
def get_prep_value(self, value):
return json.dumps(value)
def from_db_value(self, value, expression, connection):
if value is None:
return value
return json.loads(value)

If condition with a relative ref using JSON schema draft 7

I would like to use json schema to combine relative JSON pointer references, with a $ref schema, when I am introducing a conditional if / then statement.
In this case I would like to require that:
If system = Phone then require usePhone element
If system = Email then require useEmail element
The schema is generating an error when I use it to validate - I suspect the if -> $ref / enum code is the cause of the issue. The json-schema documentation suggests nesting required constant / enum values inside the defined element but I am unsure how to do this when my element is a $ref location, e.g.:
https://json-schema.org/understanding-json-schema/reference/conditionals.html
"if": {
"properties": { "country": { "const": "United States of America" } }
}
The need for a relative schema is because the instance of ContactPoint is used in multiple locations in the combined schema.
References:
https://json-schema.org/understanding-json-schema/reference/conditionals.html
https://docs.opis.io/json-schema/1.x/pointers.html
https://docs.opis.io/json-schema/1.x/conditional-subschemas.html
https://docs.opis.io/json-schema/1.x/ref-keyword.html
https://docs.opis.io/json-schema/1.x/multiple-subschemas.html
Example:
Thanks!
{
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "characteristic.entity.json",
"title": "characteristic.entity.schema.1.0",
"description": "Characteristic Objects Json Schema",
"definitions": {
"ContactPoint": {
"title": "ContactPoint",
"additionalProperties": true,
"properties": {
"id": {
"description": "",
"$ref": "primitive.entity.json#/definitions/string"
},
"type": {
"description": "The type of Contact.",
"enum": [
"Alternative",
"Primary"
]
},
"system": {
"description": "Telecommunications form for contact point - what communications system is required to make use of the contact.",
"enum": [
"Phone",
"Email",
"other"
]
},
"value": {
"description": "",
"$ref": "primitive.entity.json#/definitions/string"
},
"usePhone": {
"description": "Identifies the purpose of a Phone contact point.",
"enum": [
"Alternate",
"Business - Direct",
"Business - Main",
"Home",
"Mobile",
"Work"
]
},
"useEmail": {
"description": "Identifies the purpose of an Email contact point.",
"enum": [
"Person",
"Work",
"Business"
]
}
},
"allOf": [
{
"if": {
"$ref": "1/system",
"enum": [
"Phone"
]
},
"then": {
"required": [
"usePhone"
]
}
},
{
"if": {
"$ref": "1/system",
"enum": [
"Email"
]
},
"then": {
"required": [
"useEmail"
]
}
}
]
}
}
}
Change your "id" keywords to "$id" -- the name of that keyword changed after JSON Schema draft 4.
As #Relequestual said, you can't have sibling keywords to $ref in drafts 7 or earlier, so you should wrap the $ref in an allOf (i.e. "allOf": [ { "$ref": ... } ].
If you are using draft-2019-09, you should rename definitions to $defs.
Also, you can't use relative JSON pointers in $ref, so a ref like "1/system" will not resolve to anything (given what you have posted here). So change that ref to #/definitions/ContactPoint/properties/system and it should find the right position in the schema.

Using jsonschema to validate that a key has a unique value within an array of objects?

How do I validate JSON, with jsonschema, that within an array of objects, a specific key in each object must be unique? For example, validating the uniqueness of each Name k-v pair should fail:
"test_array": [
{
"Name": "name1",
"Description": "unique_desc_1"
},
{
"Name": "name1",
"Description": "unique_desc_2"
}
]
Using uniqueItems on test_array won't work because of the unique Description keys.
I found the alternative method of using a schema that allows arbitrary properties. The only caveat is that JSON allows duplicate object keys, but duplicates will override their previous instances. The array of objects with the key "Name" can be converted to an object with arbitrary properties:
For example, the following JSON:
"test_object": {
"name1": {
"Desc": "Description 1"
},
"name2": {
"Desc": "Description 2"
}
}
would have the following schema:
{
"type": "object",
"properties": {
"test_object": {
"type": "object",
"patternProperties": {
"^.*$": {
"type": "object",
"properties": {
"Desc": {"type" : "string"}
},
"required": ["Desc"]
}
},
"minProperties": 1,
"additionalProperties": false
}
},
"required": ["test_object"]
}

Azure Data Factory json dataset missing property in typeProperties

I am creating a dataset from json-formatted data in an Azure Data Factory (v1). When using the following code, I get a Property expected error with the infotext Property specific to this data set type on the typeProperties object. From what I can see, I am using the same properties as in the example docs. What property am I missing?
Dataset definition:
{
"name": "JsonDataSetData",
"properties": {
"type": "AzureDataLakeStore",
"linkedServiceName": "TestAzureDataLakeStoreLinkedService",
"structure": [
{
"name": "timestamp",
"type": "String"
},
{
"name": "value",
"type": "Double"
}
],
"typeProperties": {
"folderPath": "root_folder/sub_folder",
"format": {
"type": "JsonFormat",
"filePattern": "setOfObjects",
"jsonPathDefinition": {
"spid": "$.timestamp",
"value": "$.value"
}
},
},
"availability": {
"frequency": "Day",
"interval": 1
}
}
}
Have you tried adding encodingName and nestingSeparator with the default values? The documentation has mistakes sometimes and a property that is documented as not required might be giving you this error.

How to define JSON schema for object that holds Properties object?

I need to create a JSON schema for object that will include java Properties object as one of its properties.
The nested Properties object will be simply list of key=value. Both key and value are of type string.
I failed to find any docs that describe how to define the schema that includes 2 new types.
shall it be something like:
{
"type": "object",
"name": "MyObj",
"properties": {
"prop1": {
"type": "string",
"description": "prop1",
"required": true
},
"props": {
"type": "array",
"items": {
"type": "object"
"properties": {
"key": {
"type": "string",
"description": "key",
"required": true
},
"value": {
"type": "string",
"description": "the value",
"required": true
}
}
"description": "the value",
"required": true
}
}
}
}
The schema you have written (assuming the commas are fixed) describes data of the form:
{
"prop1": "Some string property goes here",
"props": [
{"key": "foo", "value": "bar"},
{"key": "foo2", "value": "bar2"},
...
]
}
If this is what you wanted, then you are already finished.
However, I do wonder why you are using key/value pairs in an array, when you could use a JSON object with string keys instead. Using the additionalProperties keyword, you could have a schema:
{
"type": "object",
"name": "MyObj",
"properties": {
"prop1": {
"type": "string",
"description": "prop1"
},
"props": {
"type": "object",
"additionalProperties": {
"type": "string",
"description": "string values"
}
}
}
}
This describes a data format like:
{
"prop1": "Some string property goes here",
"props": {
"foo": "bar",
"foo2": "bar2"
}
}
At W3 schools (JSON Syntax) you can read how the array should be defined.
There is no schema like the xsd for xml, however i've found an approach on json-schema.org. If you are able to, i'll advice to youse google-GSON library for JSON. You could Store key Value as "id" : "value" and build only one object, containing all requieed pairs:
{ "lang" : "EN" , "color" : "red" }
Your posted model is incorect, you can check it on jsonlint.com
Here is a working version, i'm not sure if the modell is as expected.
{
"type": "object",
"name": "MyObj",
"properties": [
{
"prop1": {
"type": "string",
"description": "prop1",
"required": true
},
"props": {
"type": "array",
"items": {
"type": "object",
"properties": {
"key": {
"type": "string",
"description": "key",
"required": true
},
"value": {
"type": "string",
"description": "the value",
"required": true
}
},
"description": "the value",
"required": true
}
}
}
]
}