Skip to content
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

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

Open
fmaritato opened this issue Apr 29, 2020 · 4 comments
Open

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

fmaritato opened this issue Apr 29, 2020 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@fmaritato
Copy link

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
Copy link
Owner

@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 leadpony self-assigned this Apr 30, 2020
@fmaritato
Copy link
Author

fmaritato commented Apr 30, 2020

@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
Copy link
Owner

leadpony commented May 2, 2020

@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 leadpony added the enhancement New feature or request label May 2, 2020
@jdimeo
Copy link

jdimeo commented Jul 8, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants