Comments (12)
Hi,
This is not a bug report and parser module won't change the way the Document has been described.
If you need to get the content of a $ref, you have two ways:
[model].getReference(context).getMappedContent(clazz.class);
[model].copy(context, true);
from openapi4j.
Thank you, the following worked for me;
Path path = api.getPath(urlPath);
Operation op = null;
if (path.isRef()) {
op = path.copy(api.getContext(), true).getOperation(method);
}
else {
op = path.getOperation(method);
}
validator = new OperationValidator(validationContext, api, path, op);
validator.validateBody..etc
I found it was necessary to use the original 'path' when creating the 'OperationValidator'.
If path itself wasn't a ref, but had parameters that referenced schemas in other files, would the above still work ok? Do you have to do a recursive check through your model looking for a 'isRef' to ensure you have a working validator?
from openapi4j.
Yes, it should work for variables in path as the registry is available from the context.
Internally, OperationValidator
makes flat all the description for the current operation to simplify the process and keep best performance for each further request/response.
from openapi4j.
Great, thanks. I'll close the issue.
from openapi4j.
I've been experimenting with this setup and the use of discriminators. It seems the "path.copy" does not perform a deep copy when it comes to discriminators. i.e the example listed at: "https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/" does not work if it is in another file, referenced via $ref for a path. It does work if it is the 'main' file however.
It also works ok if the 'discriminator: propertyName' keywords aren't used.
An exception is thrown when trying to construct the 'OperationValidator'.
Could there be a workaround to this problem?
java.lang.NullPointerException
at org.openapi4j.schema.validator.v3.ReferenceValidator.<init>(ReferenceValidator.java:47)
at org.openapi4j.schema.validator.v3.ValidatorsRegistry.getValidators(ValidatorsRegistry.java:65)
at org.openapi4j.schema.validator.v3.SchemaValidator.read(SchemaValidator.java:171)
at org.openapi4j.schema.validator.v3.SchemaValidator.<init>(SchemaValidator.java:86)
at org.openapi4j.schema.validator.v3.DiscriminatorValidator.setupAnyOneOfDiscriminatorSchemas(DiscriminatorValidator.java:164)
at org.openapi4j.schema.validator.v3.DiscriminatorValidator.setupDiscriminator(DiscriminatorValidator.java:114)
at org.openapi4j.schema.validator.v3.DiscriminatorValidator.<init>(DiscriminatorValidator.java:54)
at org.openapi4j.schema.validator.v3.OneOfValidator.<init>(OneOfValidator.java:34)
at org.openapi4j.schema.validator.v3.ValidatorsRegistry.getValidators(ValidatorsRegistry.java:65)
at org.openapi4j.schema.validator.v3.SchemaValidator.read(SchemaValidator.java:171)
at org.openapi4j.schema.validator.v3.SchemaValidator.<init>(SchemaValidator.java:86)
at org.openapi4j.schema.validator.v3.SchemaValidator.<init>(SchemaValidator.java:63)
at org.openapi4j.operation.validator.validation.BodyValidator.initValidator(BodyValidator.java:69)
at org.openapi4j.operation.validator.validation.BodyValidator.<init>(BodyValidator.java:35)
at org.openapi4j.operation.validator.validation.OperationValidator.createBodyValidators(OperationValidator.java:374)
at org.openapi4j.operation.validator.validation.OperationValidator.createResponseBodyValidators(OperationValidator.java:360)
at org.openapi4j.operation.validator.validation.OperationValidator.<init>(OperationValidator.java:134)
at org.openapi4j.operation.validator.validation.OperationValidator.<init>(OperationValidator.java:86)
at xxxxx.createValidator(xxx)
openapi: 3.0.3
info:
description: x
title: x
version: '1.0'
servers:
- url: 'http://localhost:80/api'
paths:
/v1/xxxx:
post:
responses:
'200':
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/StringObjType'
- $ref: '#/components/schemas/DoubleObjType'
- $ref: '#/components/schemas/Base64ObjType'
# discriminator:
# propertyName: objectType
# mapping:
# string: '#/components/schemas/StringObjType'
# double: '#/components/schemas/DoubleObjType'
# base64: '#/components/schemas/Base64ObjType'
description: conversion response
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/RequestType'
description: conversion request
components:
schemas:
RequestType:
properties:
type:
type: string
required:
- type
type: object
StringObjType:
properties:
value:
type: string
objectType:
type: string
required:
- objectType
type: object
DoubleObjType:
properties:
value:
type: number
format: double
boolcheck:
type: boolean
objectType:
type: string
required:
- objectType
type: object
Base64ObjType:
properties:
value:
type: string
format: base64
objectType:
type: string
required:
- objectType
type: object
from openapi4j.
Can you provide a reproducer?
If the reference content is reachable and validated at parsing time this should work.
Also, did you provide the same OAIContext to the OperationValidator?
from openapi4j.
The root openapi.json file simply refers to the one above;
{
"openapi": "3.0.3",
"info": {
"description": "openapi test spec",
"title": "xxx",
"version": "1.0"
},
"servers": [
{
"url": "http://localhost:80/api"
}
],
"paths": {
"/v1/xxxx": {
"$ref": "other_file.yaml#/paths/~1v1~1xxxx"
}
}
}
The code is more or less;
OpenApi3 api api = new OpenApi3Parser().parse(specFile, false);
ValidationContext<OAI3> validationContext = new ValidationContext<OAI3>(api.getContext());
Path path = api.getPath(urlPath);
Operation op = null;
if (path.isRef()) {
op = path.copy(validationContext.getContext(), true).getOperation(method);
}
else {
op = path.getOperation(method);
}
validator = new OperationValidator(validationContext, api, path, op);
validator.validateBody..etc
I believe I am passing the same context through.
from openapi4j.
copy method of a schema will never copy flatten content for collections schemas when there's a discriminator since it only works with referenced schemas.
Please, give me a reproducer to allow further digging.
from openapi4j.
The following code reproduces the issue. Removing the 'discriminator' and it parses ok. Moving the contents into a single file also parses ok.
I assume this relates to your comment:
copy method of a schema will never copy flatten content for collections schemas when there's a discriminator since it only works with referenced schemas.
But is there a way to achieve what I'm doing below? I want to have an 'OperationValidator' tied to each route that I support so that I can easily call validateBody etc on the request/response. I dynamically register routes at startup and associate them with appropriate "operation validators".
import org.openapi4j.core.model.v3.OAI3;
import org.openapi4j.operation.validator.validation.OperationValidator;
import org.openapi4j.parser.OpenApi3Parser;
import org.openapi4j.parser.model.v3.OpenApi3;
import org.openapi4j.parser.model.v3.Operation;
import org.openapi4j.parser.model.v3.Path;
import org.openapi4j.schema.validator.ValidationContext;
import java.io.File;
public class main {
public static void main(String[] args) throws Exception {
File specFile = new File("./openapi.yaml");
OpenApi3 api = new OpenApi3Parser().parse(specFile, false);
ValidationContext<OAI3> validationContext = new ValidationContext<OAI3>(api.getContext());
Path path = api.getPath("/v1/xxxx");
Operation op = null;
if(path.isRef()) {
op = path.copy(validationContext.getContext(), true).getOperation("post");
} else {
op = path.getOperation("post");
}
OperationValidator validator = new OperationValidator(validationContext, api, path, op);
}
}
With the following files:
openapi.yaml:
openapi: 3.0.3
info:
description: openapi test spec
title: xxx
version: '1.0'
servers:
- url: 'http://localhost:80/api'
paths:
/v1/xxxx:
$ref: 'other_file.yaml#/paths/~1v1~1xxxx'
other_file.yaml:
openapi: 3.0.3
info:
description: x
title: x
version: '1.0'
servers:
- url: 'http://localhost:80/api'
paths:
/v1/xxxx:
post:
responses:
'200':
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/StringObjType'
- $ref: '#/components/schemas/DoubleObjType'
discriminator:
propertyName: objectType
# mapping:
# string: '#/components/schemas/StringObjType'
# double: '#/components/schemas/DoubleObjType'
# base64: '#/components/schemas/Base64ObjType'
description: conversion response
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/RequestType'
description: conversion request
components:
schemas:
RequestType:
properties:
type:
type: string
required:
- type
type: object
StringObjType:
properties:
value:
type: string
objectType:
type: string
required:
- objectType
type: object
DoubleObjType:
properties:
value:
type: number
format: double
objectType:
type: string
required:
- objectType
type: object
from openapi4j.
Closing this and opened those two issues #91 and #93
from openapi4j.
The following workaround doesn't seem to work anymore with version 1.0:
if(path.isRef()) {
// op = path.copy(validationContext.getContext(), true).getOperation("post");
op = path.copy().getOperation("post");
} else {
op = path.getOperation("post");
}
Using the below works however:
op = path.getReference(validationContext.getContext()).getMappedContent(Path.class).getOperation(method);
But is this creating excessive copies of the schema parts for every different path?
from openapi4j.
You mean with getMappedContent
method?
No, this method maps the content only once from the raw content and return cached version on further calls.
So now, your adapted code does the exactly the same as copy(model, true)
in a more efficient way (by avoiding recursion and copy each time) but you have to be careful if Operation is a $ref too.
from openapi4j.
Related Issues (20)
- int64 or int32 format not identified by openapi4j tool HOT 3
- Mandatory element missing is not throwing error HOT 3
- Some of the optional fields when using oneOf are not being validated HOT 1
- Convenience methods to support easy response validation HOT 3
- Spring Adapter
- Validating data against schema with $ref HOT 2
- Incorrect path pattern for path parameter HOT 2
- Creating schema references programatically HOT 7
- OpenApi3Parser - add ability to provide spec string/stream to parse method HOT 2
- Integer formats not identified in 1.0.5 version openapi4j tool HOT 1
- Scheme that contains path parameters with minus sign in name fails validation HOT 1
- Throw ValidationException on path pattern collision
- Give the option to throw a validation error with no response validator is found
- RequestValidator fails when path params contains minus char HOT 1
- Get the path of an operation HOT 2
- ADDITIONAL_PROPS_RESTRICT = true rejects all attributes HOT 1
- Duplicate properties, Duplicate Headers are not being validated HOT 2
- Remove toString() call to replace NPE with more meaningful message
- Validation of callback URLs can't handle allOf/oneOf/... HOT 3
- Path pattern collision leads to incorrect Path selection
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openapi4j.