Skip to content

Commit

Permalink
Add id and schema keywords when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
carlesarnal committed Apr 29, 2024
1 parent 58635f2 commit 2637cd3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static Map<String, ContentHandle> recursivelyResolveReferences(List<Artif
* @return the main content rewritten to use the full coordinates of the artifact version and the full tree of dependencies, also rewritten to use coordinates instead of the reference name.
*/
public static RewrittenContentHolder recursivelyResolveReferencesWithContext(ContentHandle mainContent, String mainContentType, List<ArtifactReferenceDto> references,
Function<ArtifactReferenceDto, ContentAndReferencesDto> loader) {
Function<ArtifactReferenceDto, ContentAndReferencesDto> loader) {
if (references == null || references.isEmpty()) {
return new RewrittenContentHolder(mainContent, Collections.emptyMap());
}
Expand All @@ -75,8 +75,10 @@ public static RewrittenContentHolder recursivelyResolveReferencesWithContext(Con
* Re-writes each schema node content to use the full coordinates of the artifact version instead of just using the original reference name.
* This allows to dereference json schema artifacts where there might be duplicate file names in a single hierarchy.
*/
private static RewrittenContentHolder resolveReferencesWithContext(ContentHandle mainContent, String schemaType, Map<String, ContentHandle> partialRecursivelyResolvedReferences,
List<ArtifactReferenceDto> references, Function<ArtifactReferenceDto, ContentAndReferencesDto> loader) {
private static RewrittenContentHolder resolveReferencesWithContext(ContentHandle mainContent, String schemaType,
Map<String, ContentHandle> partialRecursivelyResolvedReferences,
List<ArtifactReferenceDto> references,
Function<ArtifactReferenceDto, ContentAndReferencesDto> loader) {
Map<String, String> referencesRewrites = new HashMap<>();
if (references != null && !references.isEmpty()) {
for (ArtifactReferenceDto reference : references) {
Expand All @@ -91,9 +93,11 @@ private static RewrittenContentHolder resolveReferencesWithContext(ContentHandle
ArtifactTypeUtilProvider typeUtilProvider = ARTIFACT_TYPE_UTIL.getArtifactTypeProvider(nested.getArtifactType());
RewrittenContentHolder rewrittenContentHolder = resolveReferencesWithContext(nested.getContent(), nested.getArtifactType(),
partialRecursivelyResolvedReferences, nested.getReferences(), loader);
String referenceCoordinates = concatArtifactVersionCoordinates(reference.getGroupId(), reference.getArtifactId(), reference.getVersion());
String referenceCoordinates = concatArtifactVersionCoordinatesWithRefName(reference.getGroupId(), reference.getArtifactId(),
reference.getVersion(), reference.getName());
referencesRewrites.put(reference.getName(), referenceCoordinates);
ContentHandle rewrittenContent = typeUtilProvider.getContentDereferencer().rewriteReferences(rewrittenContentHolder.getRewrittenContent(), referencesRewrites);
ContentHandle rewrittenContent = typeUtilProvider.getContentDereferencer()
.rewriteReferences(rewrittenContentHolder.getRewrittenContent(), referencesRewrites);
partialRecursivelyResolvedReferences.put(referenceCoordinates, rewrittenContent);
}
}
Expand Down Expand Up @@ -347,8 +351,8 @@ public static boolean notEmpty(Collection<?> collection) {
return collection != null && !collection.isEmpty();
}

public static String concatArtifactVersionCoordinates(String groupId, String artifactId, String version) {
return groupId + ":" + artifactId + ":" + version;
public static String concatArtifactVersionCoordinatesWithRefName(String groupId, String artifactId, String version, String referenceName) {
return groupId + ":" + artifactId + ":" + version + ":" + referenceName;
}

public static class RewrittenContentHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.apicurio.registry.content.dereference;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -27,10 +28,12 @@
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import io.apicurio.registry.content.ContentHandle;
import io.vertx.core.json.JsonObject;
import io.vertx.json.schema.Draft;
import io.vertx.json.schema.JsonSchema;
import io.vertx.json.schema.JsonSchemaOptions;
import io.vertx.json.schema.SchemaRepository;
import io.vertx.json.schema.impl.JsonRef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -44,6 +47,9 @@
public class JsonSchemaDereferencer implements ContentDereferencer {

private static final ObjectMapper objectMapper;
private static final Logger log = LoggerFactory.getLogger(JsonSchemaDereferencer.class);
private static final String idKey = "$id";
private static final String schemaKey = "$schema";

static {
objectMapper = new ObjectMapper();
Expand All @@ -60,11 +66,38 @@ public class JsonSchemaDereferencer implements ContentDereferencer {
public ContentHandle dereference(ContentHandle content, Map<String, ContentHandle> resolvedReferences) {
//Here, when using rewrite, I need the new reference coordinates, using the full artifact coordinates
// and not just the reference name and the old name, to be able to do the re-write.
SchemaRepository schemaRepository = SchemaRepository.create(new JsonSchemaOptions().setBaseUri("https://test.com"));
String id = null;
String schema = null;

try {
JsonNode contentNode = objectMapper.readTree(content.content());
id = contentNode.get(idKey).asText();
schema = contentNode.get(schemaKey).asText();
}
catch (JsonProcessingException e) {
log.warn("No schema or id provided for schema");
}

JsonSchemaOptions jsonSchemaOptions = new JsonSchemaOptions()
.setBaseUri("http://localhost");

if (null != schema) {
jsonSchemaOptions.setDraft(Draft.fromIdentifier(schema));
}

Map<String, JsonSchema> lookups = new HashMap<>();
resolveReferences(resolvedReferences, lookups);
JsonObject resolvedSchema = JsonRef.resolve(new JsonObject(content.content()), lookups);
return ContentHandle.create(JsonSchema.of(schemaRepository.resolve(resolvedSchema)).toString());

if (null != id) {
resolvedSchema.put(idKey, id);
}

if (schema != null) {
resolvedSchema.put(schemaKey, schema);
}

return ContentHandle.create(resolvedSchema.encodePrettily());
}

private void resolveReferences(Map<String, ContentHandle> resolvedReferences, Map<String, JsonSchema> lookups) {
Expand Down

0 comments on commit 2637cd3

Please sign in to comment.