Closed kristof-mattei closed 3 years ago
Hey @Kristof-Mattei. Thanks for reaching out.
Looking at your first code example I see what is causing the problem. Here is your code example for reference:
const Enforcer = require('openapi-enforcer')
Enforcer("./reproduce.yaml", { fullResult: true, useNewRefParser: true }).then(openapi => {
const x = new Enforcer.v3_0.Schema(openapi.value.components.schemas.GetSomething)
console.log(x.error);
})
On line 4 you attempt to create a Schema component instance by using openapi.value.components.schemas.GetSomething
. The problem is that the openapi
parameter from line 3 is already a fully built tree of instantiated components.
In other words, you shouldn't try to create a new Schema component instance from openapi.value.components.schemas.GetSomething
because it already is a new Schema component instance.
When a component definition is turned into an enforcer component instance some properties and methods are applied to it. So, when you passed an instance of the Schema instead of a Schema definition it had properties that would cause it to be invalid.
Here is your example corrected:
const Enforcer = require('openapi-enforcer')
Enforcer("./reproduce.yaml", { fullResult: true, useNewRefParser: true }).then(openapi => {
const x = openapi.value.components.schemas.GetSomething
})
Let me know if this does not fix it for you. I'll close this issue unless I hear otherwise that it's still a problem, but feel free to keep asking questions on this thread as needed.
@Gi60s Thank you for the swift explanation!
I'm hitting the weirdest error when trying to use
openapi-enforcer
where I get errors validating a Schema. This is the test code:This is the schema:
If we run this code we get the following error:
Now I don't understand the error, I'm not using those properties.
BUT, there are some things that I can do to make them go away, which make me believe this is a library bug.
I can restructure the schema as follows:
Works like a charm! In this all I did was move
BasePointer
to the top.Now, I can understand there is some issue with resolving refs, and this is causing the bug, BUT, if we take our original schema, and REMOVE something that isn't used, i.e. the
JsonApiBaseSingleResponse
and leaveBasePointer
at the bottom it ALSO works:So I'm not sure what is going wrong here.
Notice that this is a greatly reduced testcase just to show the bug. In reality what I'm trying to do is specifying a base type of objects for the items in the array of
GetSomething
, and the 2nd item in theallOf
inGetSomething
actually specifies a an object derived ofBasePointer
with more attributes.