-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove confusing path replacement #14
base: master
Are you sure you want to change the base?
Conversation
Hello, thank you for the PR. This code is actually needed for proper file dereferencing. Can you please provide sample schemas and code that reproduces the problem. |
+1 Having the same issue. Everything works as expected then the |
Hello, thanks for the input. The issue with this PR is that it would break existing functionality and cause failing tests. That would have to be addressed as well as we would need tests for whatever situation this PR is suppose to fix. I am still trying to understand the issue. Can you please explain in detail and provide some sample schemas and code. I've tried to reproduce the issue from original post and everything works fine for me: structure:
and when I run it it outputs:
as expected. |
I lost this issue out of focus, but taken from our current state of code (with this fix in place) this is a real life example we're actually using.
{
"database": {
"database": "some_db",
"username": "test",
"password": "test",
"host": "localhost",
"port": 5432
}
}
{
"database": {
"$ref": "shared.json#database"
}
} Now the goal was to be able to reference any json in the whole config folder from any other json in the same folder. As you can see we have the environment the config is targeted at as an extra folder in the config structure (in this case development). This folder serves as our root folder for any schema references, because relative paths would be too hard to maintain. I might find some time on the weekend to put together a full test for a more complex scenario with a deeper folder structure. |
Indeed, just removing a block of code isn't a viable solution as @bojand mentioned. Actually the current case where this is happening seems to be caused by having other refs in the file that's originally referenced ( |
Okay @bojand, got an example for you here :)
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://example.com/object/example-object",
"properties": {
"attribute": {
"$ref": "shared/shared-object.json#/definitions/objectAttribute"
}
}
}
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://example.com/shared/shared-definitions",
"definitions": {
"problematicDefinition": {
"type": "number"
}
}
}
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://example.com/shared/shared-object",
"definitions": {
"objectAttribute": {
"type": "string"
}
},
"properties": {
"problematic": {
"$ref": "shared/shared-definitions.json#/definitions/problematicDefinition"
}
}
}
'use strict';
const path = require('path');
const deref = require('json-schema-deref');
const schema = require('./objects/example-object.json');
const options = {
failOnMissing: true,
baseFolder: path.resolve('./')
}
deref(schema, options, (error, fullSchema) => {
console.log(error);
console.log(JSON.stringify(fullSchema, null, 2));
}); output, unmodified json-schema-deref:
output, json-schema-deref modified as in #14:
So the problem in this case seems to be that |
Hmm the way we do file resolution is simply using {
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://example.com/object/example-object",
"properties": {
"attribute": {
"$ref": "../shared/shared-object.json#/definitions/objectAttribute"
}
}
} and then |
@bojand All the refs have paths relative to the The problem is that |
@nanuuki That's exactly what I was looking for. Have one base path and let all references - no matter how deep - use paths relative to that very origin. |
I want to use following folder structure:
config
+-- user
---- +-- shared.json
---- +-- user.json
---- +-- .....
+-- shared.json
In this structure user.json can reference user/shared.json and user/shared.json can reference config/shared.json. When the base directory changes by referencing another file it causes the behavior to be unpredictable. With a fixed base directory this problem doesn't occur for me.