-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port json schema objects, asyncapi and openapi dereferencing (#5315)
* Add test for json schema object and property level reference * Add object and property level tests for json schema * Add test with multiple references to a single file * Port openapi dereference
- Loading branch information
1 parent
ef1c9cc
commit 75c5c39
Showing
17 changed files
with
1,013 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...util/openapi/src/main/java/io/apicurio/registry/content/dereference/ReferenceInliner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2023 Red Hat Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.apicurio.registry.content.dereference; | ||
|
||
import com.fasterxml.jackson.core.JsonPointer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import io.apicurio.datamodels.Library; | ||
import io.apicurio.datamodels.models.Node; | ||
import io.apicurio.datamodels.models.Referenceable; | ||
import io.apicurio.datamodels.models.visitors.AllNodeVisitor; | ||
import io.apicurio.registry.content.ContentHandle; | ||
import io.apicurio.registry.content.TypedContent; | ||
import io.apicurio.registry.content.refs.JsonPointerExternalReference; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Inlines all references found in the data model. | ||
* | ||
* @author [email protected] | ||
*/ | ||
public class ReferenceInliner extends AllNodeVisitor { | ||
private static final Logger logger = LoggerFactory.getLogger(ReferenceInliner.class); | ||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
private final Map<String, TypedContent> resolvedReferences; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param resolvedReferences | ||
*/ | ||
public ReferenceInliner(Map<String, TypedContent> resolvedReferences) { | ||
this.resolvedReferences = resolvedReferences; | ||
} | ||
|
||
/** | ||
* @see AllNodeVisitor#visitNode(Node) | ||
*/ | ||
@Override | ||
protected void visitNode(Node node) { | ||
if (node instanceof Referenceable) { | ||
String $ref = ((Referenceable) node).get$ref(); | ||
if ($ref != null && resolvedReferences.containsKey($ref)) { | ||
inlineRef((Referenceable) node); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Inlines the given reference. | ||
* | ||
* @param refNode | ||
*/ | ||
private void inlineRef(Referenceable refNode) { | ||
String $ref = refNode.get$ref(); | ||
|
||
JsonPointerExternalReference refPointer = new JsonPointerExternalReference($ref); | ||
ContentHandle refContent = resolvedReferences.get($ref).getContent(); | ||
|
||
// Get the specific node within the content that this $ref points to | ||
ObjectNode refContentNode = getRefNodeFromContent(refContent, refPointer.getComponent()); | ||
if (refContentNode != null) { | ||
// Read that content *into* the current node | ||
Library.readNode(refContentNode, (Node) refNode); | ||
|
||
// Set the $ref to null (now that we've inlined the referenced node) | ||
refNode.set$ref(null); | ||
} | ||
} | ||
|
||
private ObjectNode getRefNodeFromContent(ContentHandle refContent, String refComponent) { | ||
try { | ||
ObjectNode refContentRootNode = (ObjectNode) mapper.readTree(refContent.content()); | ||
if (refComponent != null) { | ||
JsonPointer pointer = JsonPointer.compile(refComponent.substring(1)); | ||
JsonNode nodePointedTo = refContentRootNode.at(pointer); | ||
if (!nodePointedTo.isMissingNode() && nodePointedTo.isObject()) { | ||
return (ObjectNode) nodePointedTo; | ||
} | ||
} else { | ||
return refContentRootNode; | ||
} | ||
} catch (Exception e) { | ||
logger.error("Failed to get referenced node from $ref content.", e); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...il/util-provider/src/test/resources/io/apicurio/registry/content/dereference/address.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"openapi": "3.0.2", | ||
"info": { | ||
"title": "address.json", | ||
"version": "1.0.0", | ||
"description": "" | ||
}, | ||
"paths": { | ||
"/": {} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"Address": { | ||
"title": "Root Type for Address", | ||
"description": "", | ||
"type": "object", | ||
"properties": { | ||
"address1": { | ||
"type": "string" | ||
}, | ||
"city": { | ||
"type": "string" | ||
}, | ||
"state": { | ||
"type": "string" | ||
}, | ||
"zip": { | ||
"type": "string" | ||
} | ||
}, | ||
"example": { | ||
"address1": "225 West South St", | ||
"city": "Springfield", | ||
"state": "Massiginia", | ||
"zip": "11556" | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.