A better way to resolve ids.

19 views
Skip to first unread message

Sri Swayampakula

unread,
Jun 3, 2015, 12:55:54 PM6/3/15
to jsonschema...@googlegroups.com
Hi Everyone,

Thank you for the great library. I have an issue with resolving ids which are urls. The problem is that I am not hosting the json schema at that url specified by the Id (I am  treating it as URI with may not exist clause). For e.g.,

GPS.json 

{
"id": "https://example.com/locator-api/1.0/default/GPS.json",
"title": "GPS",
"description": "Provides the longitude and latitude information",
"type": "object",
"properties": {
"latitude": {
"type": "integer",
"description": "Provides the latitude information of a location"
},
"longitude": {
"type": "integer",
"description": "Provides the longitude information of a location"
}
},
"required": [
"latitude",
"longitude"
]
}

and Landmark.json

{
"id": "https://example.com/locator-api/1.0/default/Landmark.json",
"title": "Landmark",
"description": "Provides the landmark related information",
"type": "object",
"properties": {
"id": {
"type": "integer",
"description": "Represents the identifier for the landmark"
},
"name": {
"type": "string",
"description": "Represents the name of the landmark"
},
"location": {
"$ref": "https://example/locator-api/1.0/default/GPS.json",
"description": "Represents the longitude and latitude information"
}
},
"required": [
"location",
"name"
]
}

The problem is that resolver is trying to reach the url causing exceptions and to avoid that I've created custom schema source like this 

// defined additional properties likes this
private final String prefixURL = "https://example.com/locator-api/1.0/";
private final String prefixFileUrl = "file:///var/tmp/location_schema/"; // This is specified in source paths


and overridden the method

public synchronized Schema create(URI id) {

if (!schemas.containsKey(id)) {
JsonNode content;

if (id.toString().startsWith(prefixURL)) {
URI fileId = null;
try {
fileId = new URI(id.toString().replace(prefixURL, prefixFileUrl));
} catch (URISyntaxException e) {
e.printStackTrace();
}
content = contentResolver.resolve(removeFragment(fileId));
}

else {
content = contentResolver.resolve(removeFragment(id));
}

if (id.toString().contains("#")) {
content = fragmentResolver.resolve(content, '#' + substringAfter(id.toString(), "#"));
}

schemas.put(id, new Schema(id, content));
}

return schemas.get(id);
}

I could easily solve the problem but I am not a great fan of the solution. I would like to know if there is any easy way to solve this.

Thanks,




Reply all
Reply to author
Forward
0 new messages