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

Method to get JsonNode with internal $ref pointers deferenced #43

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,9 @@ public class Test {

}
```
## Get JsonNode of a complete deferenced JsonNode
```
OpenApi3 model = (OpenApi3) new OpenApiParser().parse(url);
model.validate();
JsonNode mySchema = ((SchemaImpl)model.getPath("/pets").getPost().getResponse("200").getContentMediaType("application/json").getSchema()).getDereferencedJsonTree();
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
*******************************************************************************/
package com.reprezen.kaizen.oasparser.jsonoverlay;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Optional;
import com.reprezen.kaizen.oasparser.jsonoverlay.coll.ObjectOverlay;

public abstract class JsonOverlay<V> {

Expand Down Expand Up @@ -77,12 +81,56 @@ public V get() {
public void set(V value) {
this.value = value;
}

private JsonNode internalDeferenceRecursive(JsonNode node) {
if (node != null) {
if (node.isObject() && node.has("$ref")) {
//Parse reference
Iterator<ResolutionBase> i = ResolutionBase.getAllBases().iterator();
while(i.hasNext()) {
JsonNode candidate = Reference.get(node.get("$ref").asText(), i.next(), true).getJson();
if (candidate != null && !candidate.isMissingNode()) {
return this.internalDeferenceRecursive(candidate);
}
}
} else if (node.isContainerNode()) {
if (node.isArray()) {
ArrayNode arrayNode = (ArrayNode)node;
for (int i = 0; i < arrayNode.size(); i++) {
JsonNode expectedChangedNode = this.internalDeferenceRecursive(arrayNode.get(i));
if (arrayNode.get(i) != expectedChangedNode /* Simple check of instance */) {
arrayNode.set(i, expectedChangedNode);
}
}
} else if (node.isObject()) {
ObjectNode objectNode = (ObjectNode)node;
Iterator<String> fieldNameIterator = objectNode.fieldNames();
while (fieldNameIterator.hasNext()) {
String fieldName = fieldNameIterator.next();
JsonNode expectedChangedNode = this.internalDeferenceRecursive(objectNode.get(fieldName));
if (objectNode.get(fieldName) != expectedChangedNode /* Simple check of instance */) {
objectNode.set(fieldName, expectedChangedNode);
}
}
}
}
}
return node;
}

public JsonNode getDereferencedJsonTree() {
if (ref != null) {
return internalDeferenceRecursive(resolvedJson);
} else {
return internalDeferenceRecursive(json);
}
}

public JsonNode getJson() {
if (ref != null) {
return resolvedJson;
} else {
return json;
return json;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,16 @@ enum NumericType {
LONG(Long.class), //
SHORT(Short.class);

private static Map<Class<? extends Number>, NumericType> types = Maps.newHashMap();
private static Map<Class<? extends Number>, NumericType> types;

private NumericType(Class<? extends Number> cls) {
register(cls, this);
}

private void register(Class<? extends Number> cls, NumericType type) {
types.put(cls, type);
if (NumericType.types == null)
NumericType.types = Maps.newHashMap();
types.put(cls, type);
}

public static NumericType of(Object value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*******************************************************************************
* Copyright (c) 2017 ModelSolv, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* ModelSolv, Inc. - initial API and implementation and/or initial documentation
*******************************************************************************/
package com.reprezen.swaggerparser.test;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;

import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.yaml.snakeyaml.Yaml;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Queues;
import com.reprezen.kaizen.oasparser.OpenApiParser;
import com.reprezen.kaizen.oasparser.jsonoverlay.JsonOverlay;
import com.reprezen.kaizen.oasparser.jsonoverlay.Reference;
import com.reprezen.kaizen.oasparser.jsonoverlay.coll.CollectionOverlay;
import com.reprezen.kaizen.oasparser.jsonoverlay.coll.CollectionStore;
import com.reprezen.kaizen.oasparser.jsonoverlay.coll.ListOverlay;
import com.reprezen.kaizen.oasparser.jsonoverlay.coll.MapOverlay;
import com.reprezen.kaizen.oasparser.jsonoverlay.coll.ObjectOverlay;
import com.reprezen.kaizen.oasparser.jsonoverlay.coll.ValListOverlay;
import com.reprezen.kaizen.oasparser.jsonoverlay.coll.ValMapOverlay;
import com.reprezen.kaizen.oasparser.model3.OpenApi3;
import com.reprezen.kaizen.oasparser.ovl3.OpenApi3Impl;
import com.reprezen.kaizen.oasparser.ovl3.RequestBodyImpl;
import com.reprezen.kaizen.oasparser.ovl3.ResponseImpl;
import com.reprezen.swaggerparser.test.JsonTreeWalker.PathKey;
import com.reprezen.swaggerparser.test.JsonTreeWalker.WalkMethod;

import com.reprezen.kaizen.oasparser.ovl3.SchemaImpl;

/**
* Basic test based on BigParseTest to test pointer deferencing
*
* @author Francesco Guardiani @slinkydeveloper
*
*/

public class DerefTest extends Assert {

@Test
public void test() throws JsonProcessingException, IOException {
URL modelUrl = DerefTest.class.getResource("/models/petstore-expanded.yaml");
Object parsedYaml = new Yaml().load(modelUrl.openStream());
JsonNode tree = new YAMLMapper().convertValue(parsedYaml, JsonNode.class);
final OpenApi3 model = (OpenApi3) new OpenApiParser().parse(modelUrl);
model.validate();
JsonNode aReallyImportantNode = ((SchemaImpl)model.getPath("/pets").getPost().getResponse("200").getContentMediaType("application/json").getSchema()).getDereferencedJsonTree();
JsonNode anotherReallyImportantNode = ((ResponseImpl)model.getPath("/pets").getPost().getResponse("200")).getDereferencedJsonTree();
System.out.println(aReallyImportantNode);
System.out.println(anotherReallyImportantNode);
}
}
8 changes: 8 additions & 0 deletions kaizen-openapi-parser/src/test/resources/models/newPet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NewPet:
required:
- name
properties:
name:
type: string
tag:
type: string
25 changes: 22 additions & 3 deletions kaizen-openapi-parser/src/test/resources/models/parseTest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ paths:
- sec3: []
servers: *id001
x-foo: foo
post:
operationId: updatePetWithForm
requestBody:
$ref: '#/components/requestBodies/reqBody1'
responses:
'200':
description: Pet updated.
x-foo: foo
parameters: *id002
x-foo: foo
Expand Down Expand Up @@ -231,8 +238,16 @@ components:
x-foo: foo
schema2:
title: title
additionalProperties:
type: integer
properties:
a:
$ref: '#/components/schemas/schema3'
schema3:
title: schema3
properties:
a:
type: array
items:
type: number
responses:
resp1:
description: description
Expand All @@ -251,6 +266,10 @@ components:
requestBodies:
reqBody1:
description: description
content:
'application/json':
schema:
$ref: '#/components/schemas/schema2'
reqBody2:
description: description
headers:
Expand Down Expand Up @@ -333,4 +352,4 @@ externalDocs:
description: description
url: url
x-foo: foo
x-foo: foo
x-foo: foo
Loading