API Request validation against Swagger API schema (openapi 3.0.2) - json

Is there any java/python/javascript library that validates API request/payload against openapi 3.0.2
i looked many of the libraries but was not able to find the correct one.
There is one problem which is found when these libraries are not able to validate polymorphism and inheritance. for example oneOf, anyOf, allOf.
what is being trying to do is to validate an API request, which includes a set of fields as described by using oneOf keyword and then followed by discriminator & mapping. The mapping fields include values with allOf Keyword but the request for validating swagger fails.

I would suggest the OpenAPI Initiative Tooling page for the list of available libraries.
There's Data validators category.
A half year ago, the best validator library I found for Java was Atlassian's Swagger Request Validator.

Related

Render HAL formatted links in openapi3 json with SpringDoc

I'm running a Spring Boot REST application with Spring HATEOAS support and generating OpenAPIv3 docs with the Springdoc Maven plugin. However when I call my REST endpoints I get links the in HAL formatted JSON ("_links"). But the generated OpenAPIv3 documentation is giving me a different format for the links ("links").
How can I get the generated OpenAPIv3 docs to match the HAL formatted links?
The only resource I've found is this link: https://github.com/springdoc/springdoc-openapi/issues/446
However, the solution given there involves using spring-data-rest which I am not using (do I need to?)
I've also tried adding #EnableHyperMediaSupport which says it configures the JSON rendering, but that had no effect on the OpenAPIv3 docs.
The answer was simple enough, I needed to pull in the springdoc-openapi-hateoas dependency (https://springdoc.org/#spring-hateoas-support). After pulling this in the JSON documentation was generated correctly with no additional configuration (I did not need #EnableHypermediaSupport or spring-data-rest).
However, if you are using the Swagger UI be aware that it will automatically generate bogus 'additionalProperty' links as an example for the resource schema. This is only in the Swagger UI, if you look at the generated openapiv3 json the structure is correct (cf: https://github.com/springdoc/springdoc-openapi/issues/237). To remedy this you can provide your own example of the resource schema.

JSON Schema Validation for PL/SQL or SQL, similar to XSLT for XML Schema Validation

I am looking for the schema validation for JSON structure received during the API Invocation from ORDS. Lets say I have 10 different json tags constructing the JSON and I want to validate this JSON against pre-defined validation to check if any required parameters are missing or to validate the datatype.
I am looking for the functionality similar to XLST which is used for XML schema validation.
Please note this schema validation is required for the JSON using PL/SQL and ORDS(Oracle Rest Database Services).
Technical Components: SQL, PL/SQL, ORDS. We want something which is compatible with the mentioned components.

Generate Angular2 forms from Swagger API specification

I'm looking for a way to generate a set of Angular2 form templates from a Swagger API definition file. I want a result that will allow me to test my POST/PUT requests, and even use it in my app.
After some research I found this Angular2 form library that takes a JSON schema as input: https://github.com/makinacorpus/angular2-schema-form
So if you know of a Swagger -> JSON Schema converter that will work too.
Cheers!
So if you know of a Swagger -> JSON Schema converter that will work
too.
Swagger 2.0 supports a subset of JSON schema draft 4. This is what swagger's Schema object is. From the docs:
The following properties are taken directly from the JSON Schema
definition and follow the same specifications:
$ref - As a JSON Reference
format (See Data Type Formats for further details)
title
description (GFM syntax can be used for rich text representation)
default (Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object)
multipleOf
...
The following properties are taken from the JSON Schema definition but
their definitions were adjusted to the Swagger Specification.
items
allOf
properties
additionalProperties
It should be a fairly simple exercise to manually extract the schema from your swagger, but I don't know of any automated tool to do this. I think the fact some of the JSON schema properties have been modified by swagger may make auto conversion problematic in certain circumstances.

Is there a json validation framework in play based on a specified grammar

An automated system is going to feed the application[Play with Scala] with JSON's and the contract of the integration is that there would be no validation required on JSON's since it will be always deemed right. But for testing purposes when we seed the data more often than not we are not able to send the correct JSONs. We would like to validate the JSON's we receive based on a set of grammars. Is there a library that already does this. Or is there a better way to do this?
Example: Grammar for valid Json :
"header"->[String, mandatory],
"footer"->[String],
"someArray"->Array[String, mandatory],
"someArrayObject"->Array[
{
{"key1"->Int, mandatory},
{"key2"->String}
},
mandatory
]
and passing,
{
"header":"headerContent",
"footer":"footerContent",
"someArray":["str1", "str2"],
"someArrayObject"->[
{"key1":4, "key2":"someStringValue"},
{"key1":5, "key2":"someOtherStringValue"}
]
} // would pass
{
"header":"headerContent",
"footer":"footerContent",
"someArray":["str1", "str2"]
} // would notpass since someArrayObject though declared mandatory is not provided in the sample json
I think play-json will satisfy you play-json
In play-json you don't create a validator as it is, but a json transformer which is a validator in itself. The author of the framework wrote a series of blog-posts to show how to work with it: json-transformers
* Haven't noticed you use play) Play has play-json included by default.
You don't have to roll out your own DSLs. This is why we have schemas. Just like using XML schemas to validate your XML docs, you can define a JSON schema to validate your JSON objects. I had a similar requirement when building a RESTful web service using Play. I solved it by using the JSON Schema Validator library.
I have used the JSON Schema draft v3. The library supports draft v3 and draft v4. You can validate your schemas against possible JSON inputs using a web application that uses the same library. The web app is hosted here.
Also there are pretty nice examples that use the draft v4. You can check them out from here.
In Play 2, I have composed an action that takes the schema resource file name as input. This keeps away a lot of JSON validation code from the controller action itself.
#JsonValidate("user-register.json")
public static Result create() {
...
}
This way, all JSON Validation code stays in one place. Pretty neat :)

Parsing Jackson annotations with Swagger

By default, Swagger parses a class's data members in order to document the objects used as parameters or returned by a given web service. If you're using Jackson, the Jackson annotations provide a much more accurate description of the API.
Does anyone know an (easy) way to get Swagger to parse Jackson annotations. Perhaps an overridden parser?
Not sure if this would help, but Jackson 2.1 and later expose POJO structure as seen by Jackson itself (ObjectMapper.acceptJsonFormatVisitor), which could be used for generating different kinds of artifacts. I have written an Avro schema generator with it, for example (as part of Jackson Avro module)
As of version 1.2, Swagger can parse Jackson annotations on its own. I confirmed this using Jackson 2.1.