leadpony / justify

Justify is a JSON validator based on JSON Schema Specification and Jakarta JSON Processing API (JSON-P).
Apache License 2.0
96 stars 18 forks source link

possible to rewrite a schema with no $ref? #49

Open fmaritato opened 4 years ago

fmaritato commented 4 years ago

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!

leadpony commented 4 years ago

@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.

fmaritato commented 4 years ago

@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.

leadpony commented 4 years ago

@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

jdimeo commented 3 years ago

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?