justify icon indicating copy to clipboard operation
justify copied to clipboard

possible to rewrite a schema with no $ref?

Open fmaritato opened this issue 4 years ago • 4 comments

In order to reuse certain definitions, I have many $refs in my schemas that point to definitions in a common.json file. Is there a way to use justify to resolve all these references and rewrite a version of the schema with all the resolutions?

So instead of

"uid": {
  "$ref": "common.json#uid"
}

the rewritten schema file would have:

"uid": {
  "type": "string",
  "minLength": 1,
  "description": "user id"
}

Thanks!

fmaritato avatar Apr 29 '20 21:04 fmaritato

@fmaritato I am not sure what you exactly want to do, but have you tried Schema Resolver in the Justify Examples? That example shows how to resolve external schemas and establish a link from the referencing schema to the referenced schema while loading the referencing schema.

leadpony avatar Apr 30 '20 09:04 leadpony

@leadpony I want to take a json schema file, dereference all the $refs and rewrite the file with the actual type definitions inline. Hopefully this makes sense lol

The reason I want to do this is because I'd like to upload my schemas into confluent's schema registry, but it can't seem to resolve the $refs that point to definitions outside of the current file.

fmaritato avatar Apr 30 '20 17:04 fmaritato

@fmaritato Thank you for quick response. I grasped your request. Sorry but there is no such feature in Justify. Your request is the same as Issue #6

leadpony avatar May 02 '20 10:05 leadpony

My request is just for internal refs - I don't need to deference HTTP. Justify already did figure out internal references. This is really bad code I wrote to create a map that I could use to dereference the schema manually:

private Map<String, JsonValue> refs = new HashMap<>();

JsonSchema schema;
JsonObject schemaJson;

val service = JsonValidationService.newInstance();
val factory = service.createSchemaReaderFactoryBuilder().withDefaultSpecVersion(SpecVersion.DRAFT_07).withSpecVersionDetection(false).build();

try (val reader = factory.createSchemaReader(cdmPath)) {
	schema     = reader.read().asObjectJsonSchema();
	schemaJson = Utilities.cast(schema.toJson());
	
	// TODO: HACK HACK HACK
	for (val ref : (Collection<?>) FieldUtils.readField(reader, "references", true)) {
		SchemaReference schemaRef = Utilities.cast(FieldUtils.readField(ref, "reference", true));
		refs.put(schemaRef.getTargetId().toString(), schemaRef.getReferencedSchema().asObjectJsonSchema().toJson());
	}
}

is there a better way to dereference $ref using public APIs and not reflection?

jdimeo avatar Jul 08 '21 02:07 jdimeo