OpenAPI spec using allOf to override inherited property - jsonschema

I am attempting to run an existing OpenAPI schema through Open API Enforcer and I am getting various validation errors in the usage of allOf. One such usage is described below:
Consider the following property in a schema:
queryVersion:
allOf:
- $ref: 'VersionDefinition.yaml'
- description: >-
This is my overriding version
VersionDefinition.yaml is defined as follows:
description: >-
Some default version description.
type: string
default: '5.2'
There are two issues with the above definition:
OpenAPI Enforcer expects all schemas defined within allOf or similar keyword to start with a type definition. So, the error it spits out is:
at: queryVersion > allOf > 1
Missing required property: type
I fix that by modifying the allOf definition as follows:
queryVersion:
allOf:
- $ref: 'VersionDefinition.yaml'
- type : object
properties:
- description: >-
This is my overriding version
That eliminates the error but what should I expect to see in the generated schema? Original author indicates he is using allOf to override the property description. However, the generated schema includes this result:
"queryVersion": {
"allOf": [
{
"description": "Some default version description",
"type": "string",
"default": "5.2"
},
{
"type": "object",
"properties": [
{
"description": "This is my overriding version"
}
]
}
]
}
What I expected to see was:
"queryVersion": {
"description": "This is my overriding version",
"type": "string",
"default": "5.2"
}
I'll keep digging but any ideas?

Related

AJV - How to validate relative references

I have a json that is used as ref in another json in the same folder:
jsonBase.json
{
"type": "number",
"title": "Your salary",
"presentation": {
"inputType": "money"
}
}
jsonFinal.json
{
"$ref": "jsonBase.json",
"presentation": {
"currency": "EUR"
}
}
When using AJV validate, the schema is invalid (it's missing the required properties), ignoring the base.json.
I checked this github issue and read the docs but I still don't understand exactly what change I need to make in order for the json to be valid.
Here's the codesandbox with demo.
You can only use $ref in a schema to reference a schema. It doesn't work in JSON instances.

How to remove blocks section from Shopify schema

How could I remove blocks section from shopify schema code? I simply tried to delete it but was not able to save due to a strange error. Please see the code below:
{% schema %}
{
"name": "Featured Collection",
"settings": [
{
"type": "collection",
"id": "featured_collection",
"label": "Collection"
},
{
"type": "text",
"id": "collection_button_label",
"label": "Button Label",
"default": "Learn More"
}
],
"blocks": [
{
"type": "section",
"name": "Section",
"settings": [
]
}
],
"presets": [
{
"name": "Featured Collection",
"category": "Product"
}
]
}
{% endschema %}
I can save the above code without any error. But When I remove the code section of "blocks", I get the following error:
Error: New schema is incompatible with the current setting value. Invalid type value for block '1577470637989'. Type must be defined in schema.New schema is incompatible with the current setting value. Invalid type value for block '1577470668608'. Type must be defined in schema
The error is kind of self explanatory. The error message
Error: New schema is incompatible with the current setting value.
Invalid type value for block '1577470637989'. Type must be defined in
schema.New schema is incompatible with the current setting value.
Invalid type value for block '1577470668608'. Type must be defined in
schema
It states that new schema that you are trying to save is not compatible with existing data you have. Shopify does not know what to do with existing blocks of those types which you want to remove.
So just remove those blocks from Shopify Customizer first and then edit the schema.

JSON Schema Array Validation Woes Using oneOf

Hope I might find some help with this validation issue: I have a JSON array that can have multiple object types (video, image). Within that array, the objects have a rel field value. The case I'm working on is that there can only be one object with "rel": "primaryMedia" allowed — either a video or an image.
Here are the object representations for the image and video case, both showing the "rel": "primaryMedia".
{
"media": [
{
"caption": "Caption goes here",
"id": "ncim87659842",
"rel": "primaryMedia",
"type": "image"
},
{
"description": "Shaima Swileh arrived in San Francisco after fighting for 17 months to get a waiver from the U.S. government to be allowed into the country to visit her son.",
"headline": "Yemeni mother arrives in U.S. to be with dying 2-year-old son",
"id": "mmvo1402810947621",
"rel": "primaryMedia",
"type": "video"
}
]
}
Here's a stripped-down version of the schema I created to validate this using oneOf (assuming this will handle my case). It doesn't however, work as intended.
{
"$id": "http://example.com/schema/rockcms/article.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {},
"properties": {
"media": {
"items": {
"oneOf": [
{
"additionalProperties": false,
"properties": {
"caption": {
"type": "string"
},
"id": {
"type": "string"
},
"rel": {
"enum": [
"primaryMedia"
],
"type": "string"
},
"type": {
"enum": [
"image"
],
"type": "string"
}
},
"required": [
"caption",
"id",
"rel",
"type"
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"description": {
"type": "string"
},
"headline": {
"type": "string"
},
"id": {
"type": "string"
},
"rel": {
"enum": [
"primaryMedia"
],
"type": "string"
},
"type": {
"enum": [
"video"
],
"type": "string"
}
},
"required": [
"description",
"headline",
"id",
"rel",
"type"
],
"type": "object"
}
]
},
"type": "array"
}
}
}
Using the JSON Schema validator at https://www.jsonschemavalidator.net, the schema validates when data is correctly presented, but the doesn't work when trying to catch errors.
In the case below, headline is added for a video, and it's missing id. This should fail because headline isn't allowed on video, and id is required.
{
"media": [
{
"headline": "Yemeni mother arrives in U.S. to be with dying 2-year-old son",
"rel": "primaryMedia",
"type": "video"
}
]
}
The results I get from the validator, however, aren't entirely expected. Seems it's conflating the two object schemas in its response.
In addition to this, I've separately found that the schema will allow the population of BOTH a video and image object in media, which isn't expected.
Been trying to figure out what I've done wrong, but am stumped. Would very much appreciate some feedback, if anyone has to offer. Thanks in advance!
Validator Response:
Message: JSON is valid against no schemas from 'oneOf'.
Schema path: #/properties/media/items/oneOf
Message: Property 'headline' has not been defined and the schema does not allow additional properties.
Schema path: #/properties/media/items/oneOf/0/additionalProperties
Message: Value "video" is not defined in enum.
Schema path: #/properties/media/items/oneOf/0/properties/type/enum
Message: Required properties are missing from object: description, id.
Schema path: #/properties/media/items/oneOf/1/required
Message: Required properties are missing from object: caption, id.
Schema path: #/properties/media/items/oneOf/0/required
Your question is formed of three parts, but the first two are linked, so I'll address those, although they don't have a "solution" as such.
How can I validate that only one object in an array has a specific key, and the others do not.
You cannot do this with JSON Schema.
The set of JSON Schema keywords which are applicable to arrays do not have a means for expressing "one of the values must match a schema", but rather are applicable to either ALL of the items in a the array, or A SPECIFIC item in the array (if items is an array as opposed to an object).
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4
The validation output is not what I expect. I expect to only see the failing branch of oneOf which relates to my object type, which
is defined by the type key.
The JSON Schema specification (as of draft-7) does not specify any format for returning errors, however the error structure you get is pretty "complete" in terms of what it's telling you (and is similar to how we are specifying errors should be returned for draft-8).
Consider, the validator knows nothing about your schema or your JSON instance in terms of your business logic.
When validating a JSON instance, a validator may step through all values, and test validation rules against all applicable subschemas. Looking at your errors, both of the schemas in oneOf are applicable to all items in your array, and so all are tested for validation. If one does not satisfy the condition, the others will be tested also. The validator cannot know, when using a oneOf, what your intent was.
You MAY be able to get around this issue, by using an if / then combination. If your if schema is simply a const of the type, and your then schema is the full object type, you may get a cleaner error response, but I haven't tested this theory.
I want ALL items in the array to be one of the types. What's going on here?
From the spec...
items:
If "items" is a schema, validation succeeds if all elements in the
array successfully validate against that schema.
oneOf:
An instance validates successfully against this keyword if it
validates successfully against exactly one schema defined by this
keyword's value.
What you have done is say: Each item in the array should be valid according to [schemaA]. SchemA: The object should be valid according to on of these schemas: [the schemas inside the oneOf].
I can see how this is confusing when you think "items must be one of the following", but items applies the value schema to each of the items in the array independantly.
To make it do what you mean, move the oneOf above items, and then refactor one of the media types to another items schema.
Here's a sudo schema.
oneOf: [items: properties: type: const: image], [items: properties: type: image]
Let me know if any of this isn't clear or you have any follow up questions.

Can properties and additionalProperties take on null value in Swagger 2.0?

"DogRequest"{
"type": "object",
"properties": {
"height": {
"type": "string"
}
}
}
"DogResponse"{
"type": "object",
"properties": {
"bark": {
"type": "string"
}
}
}
Note that I don't have any required properties defined.
I noticed that when making a request, I can have height=None
However, in my response, if bark=None, swagger throws a validation error, that None is not of type "string".
What is the rule for having properties (and additionalProperties) being null, EVEN if they aren't defined as required?
I did notice that for properties defined as required, it must be the case that they exist AND are not null. And if I want to allow the property to be null, I must include "x-nullable": true.
Do I have to include "x-nullable": true for properties that are not required as well?
Why am I seeing an inconsistency?
OpenAPI 2.0 does not support null as the data type. Some tools use x-nullable: true to handle nulls, but it's not part of the OpenAPI Specification, so whether or not it will work depends on the tools you use.
Support for null was added in OpenAPI 3.0, where properties can be marked as nullable: true.

Using $ref for jsonschema in Abao

Can someone help with schemas refs in abao? How to use --schemas option? Here is simple gist https://gist.github.com/SeanSilke/e5a2f7673ad4aa2aa43ba800c9aec31b
I try to run "abao api.raml --schemas fref.json" but got error " Missing/unresolved JSON schema $refs (fref.json) in schema".
By the way the server is mocked by osprey-mock-service.
You need add id field to your JSON schemas.
For run use: abao api.raml --server http://localhost:3000 --schemas=./*.json
Example files:
api.raml
#%RAML 0.8
title: simple API
baseUri: http://localhost:3000
/song:
get:
responses:
200:
body:
application/json:
schema: !include schema.json
example: |
{
"songId": "e29b",
"songTitle": "The song",
"albumId": "18310"
}
fref.json
{
"id": "fref.json",
"type": "string"
}
schema.json
{
"$schema": "http://json-schema.org/draft-03/schema",
"id": "schema.json",
"type": "object",
"properties":{
"songId": {"$ref": "fref.json"}
},
"required": ["songId", "albumId", "songTitle"]
}