Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Add schemaPath for RAML 08 #586

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ public interface GlobalSchema
**/
SchemaString value();

/**
* The path of the schema. If the schema is inlined returns the path inside the raml document. If the schema is included returns the complete path of the included schema.
*/
String path();
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public interface BodyLike extends RAMLLanguageElement
**/
String schemaContent();

/**
* Returns the path to the schema returned by {@link #schemaContent()}. If the schema is inlined, the path inside the RAML document is returned. If the schema is included, the original path of the schema is returned. If the schema is a reference, the path of the referenced schema is returned.
*/
String schemaPath();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/
package org.raml.v2.internal.impl.commons.model;

import org.raml.v2.internal.impl.commons.nodes.ExternalSchemaTypeExpressionNode;
import org.raml.v2.internal.impl.commons.nodes.TypeExpressionNode;
import org.raml.yagi.framework.model.AbstractNodeModel;
import org.raml.yagi.framework.nodes.KeyValueNode;
import org.raml.yagi.framework.nodes.Node;
import org.raml.yagi.framework.nodes.NullNode;
import org.raml.yagi.framework.nodes.SimpleTypeNode;
import org.raml.yagi.framework.nodes.StringNode;
import org.raml.yagi.framework.model.NodeModel;
import org.raml.yagi.framework.util.NodeSelector;
Expand Down Expand Up @@ -60,4 +64,36 @@ public String schemaContent()
return null;
}

public String schemaPath()
{
final Node schemaNode = NodeSelector.selectFrom("schema/*", getNode());
final String schema;
if (schemaNode == null)
{
return null; // Not a Json or XML schema
}
else if (schemaNode instanceof TypeExpressionNode)
{
schema = ((TypeExpressionNode) schemaNode).getTypeExpressionText();
}
else
schema = null;
// For an inline schema
if (schema == null || schema.startsWith("{") || schema.startsWith("<"))
{
return schemaNode.getPath();
}

// For an schema reference,
Node rootRamlSchema = NodeSelector.selectFrom("/schemas/" + schema, getNode());
if (rootRamlSchema instanceof ExternalSchemaTypeExpressionNode)
{
return ((ExternalSchemaTypeExpressionNode) rootRamlSchema).getSchemaPath();
}
else
{
return rootRamlSchema.getPath();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.raml.v2.internal.impl.commons.model;

import org.raml.v2.internal.impl.commons.nodes.ExternalSchemaTypeExpressionNode;
import org.raml.yagi.framework.model.AbstractNodeModel;
import org.raml.yagi.framework.nodes.KeyValueNode;
import org.raml.yagi.framework.nodes.Node;
Expand Down Expand Up @@ -43,4 +44,11 @@ public StringType value()
{
return new StringType((SimpleTypeNode) node.getValue());
}

public String path()
{
if (node.getValue() instanceof ExternalSchemaTypeExpressionNode)
return ((ExternalSchemaTypeExpressionNode) node.getValue()).getSchemaPath();
return node.getPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public String getSchemaPath()
{
return this.getStartPosition().getIncludedResourceUri();
}
return this.getStartPosition().getPath();
return this.getPath();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;

import org.junit.Test;
Expand All @@ -39,11 +42,16 @@
public class SpecInterfacesV08TestCase
{

private URI externalSchemaUri;

@Test
public void full() throws IOException
{
File input = new File("src/test/resources/org/raml/v2/api/v08/full/input.raml");
assertTrue(input.isFile());
File externalSchema = new File(input.getParentFile(), "external.schema.json");
assertTrue(externalSchema.isFile());
externalSchemaUri = externalSchema.toURI().normalize();
RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(input);
assertFalse(ramlModelResult.hasErrors());
Api api = ramlModelResult.getApiV08();
Expand All @@ -60,10 +68,14 @@ private void assertApi(Api api)
assertThat(api.protocols().get(0), is("HTTP"));
assertThat(api.protocols().get(1), is("HTTPS"));

assertThat(api.schemas().size(), is(2));
assertThat(api.schemas().size(), is(3));
assertThat(api.schemas().get(0).key(), is("UserJson"));
assertThat(api.schemas().get(0).path(), is("schemas/UserJson"));
assertThat(api.schemas().get(0).value().value(), containsString("\"firstname\": { \"type\": \"string\" }"));
assertThat(api.schemas().get(1).key(), is("UserXml"));
assertThat(api.schemas().get(1).path(), is("schemas/UserXml"));
assertThat(api.schemas().get(2).key(), is("External"));
assertThat(URI.create(api.schemas().get(2).path()).normalize(), equalTo(externalSchemaUri));

assertDocumentation(api.documentation());
assertTraits(api.traits());
Expand Down Expand Up @@ -123,6 +135,7 @@ private void assertResources(List<Resource> resources)
assertThat(childrenBody.name(), is("application/json"));
assertThat(childrenBody.schemaContent(), containsString("\"firstname\": { \"type\": \"string\" }"));
assertThat(childrenBody.schema().value(), is("UserJson"));
assertThat(childrenBody.schemaPath(), is("schemas/UserJson"));

Resource childId = child.resources().get(0);
assertThat(childId.uriParameters(), hasSize(1));
Expand Down Expand Up @@ -190,18 +203,20 @@ private void assertResponses(List<Response> responses)

private void assertBody(List<BodyLike> body)
{
assertThat(body.size(), is(4));
assertThat(body.size(), is(5));

BodyLike appJson = body.get(0);
assertThat(appJson.name(), is("application/json"));
assertThat(appJson.example().value(), containsString("\"firstname\": \"tato\""));
assertThat(appJson.schema().value(), is("UserJson"));
assertThat(appJson.schemaContent(), containsString("\"firstname\": { \"type\": \"string\" }"));
assertThat(appJson.schemaPath(), is("schemas/UserJson"));

BodyLike xml = body.get(1);
assertThat(xml.name(), is("application/xml"));
assertThat(xml.schema().value(), is("UserXml"));
assertThat(xml.schemaContent(), containsString("<xs:element type=\"xs:string\" name=\"input\"/>"));
assertThat(xml.schemaPath(), is("schemas/UserXml"));

BodyLike multipart = body.get(2);
assertThat(multipart.formParameters().size(), is(2));
Expand All @@ -211,5 +226,14 @@ private void assertBody(List<BodyLike> body)
assertThat(vndJson.name(), is("application/vnd.inline+json"));
assertThat(vndJson.schema().value(), containsString("\"input\": {"));
assertThat(vndJson.schemaContent(), containsString("\"input\": {"));
assertThat(vndJson.schemaPath(), anyOf(
is("\\/top/post/body/application\\/vnd.inline+json/schema"), // for the direct body in the request
is("\\/top/post/responses/200/body/application\\/vnd.inline+json/schema"))); // the anchor body in the response

BodyLike vndExternalJson = body.get(4);
assertThat(vndExternalJson.name(), is("application/vnd.external+json"));
assertThat(vndExternalJson.schema().value(), is("External"));
assertThat(vndExternalJson.schemaContent(), containsString("\"stringProperty\": {"));
assertThat(URI.create(vndExternalJson.schemaPath()).normalize(), equalTo(externalSchemaUri));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private JsonNode filterNodes(JsonNode jsonNode)

protected String[] getKeysToFilter()
{
return new String[] {"schemaPath"};
return new String[] {"schemaPath", "path"};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json-schema.org/draft-04/schema",
"type": "object",
"properties": {
"stringProperty": {
"type": "string"
}
},
"required": [
"stringProperty"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ schemas:
</xs:complexType>
</xs:element>
</xs:schema>

- External: !include external.schema.json
traits:
one:
description: method description from trait one
Expand Down Expand Up @@ -107,6 +109,8 @@ resourceTypes:
"required": false,
"type": "object"
}
application/vnd.external+json:
schema: External
responses:
200:
body: *bodyPost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@
"value": "{\n \"$schema\": \"http://json-schema.org/draft-03/schema\",\n \"properties\": {\n \"input\": {\n \"required\": false,\n \"type\": \"string\"\n }\n },\n \"required\": false,\n \"type\": \"object\"\n}\n"
},
"schemaContent": "{\n \"$schema\": \"http://json-schema.org/draft-03/schema\",\n \"properties\": {\n \"input\": {\n \"required\": false,\n \"type\": \"string\"\n }\n },\n \"required\": false,\n \"type\": \"object\"\n}\n"
},
{
"description": null,
"example": null,
"formParameters": [],
"name": "application/vnd.external+json",
"schema": {
"value": "External"
},
"schemaContent": "{\n\t\"$schema\": \"http://json-schema.org/draft-04/schema\",\n\t\"type\": \"object\",\n\t\"properties\": {\n\t\t\"stringProperty\": {\n\t\t\t\"type\": \"string\"\n\t\t}\n\t},\n\t\"required\": [\n\t\t\"stringProperty\"\n\t]\n}"
}
],
"description": {
Expand Down Expand Up @@ -268,6 +278,16 @@
"value": "{\n \"$schema\": \"http://json-schema.org/draft-03/schema\",\n \"properties\": {\n \"input\": {\n \"required\": false,\n \"type\": \"string\"\n }\n },\n \"required\": false,\n \"type\": \"object\"\n}\n"
},
"schemaContent": "{\n \"$schema\": \"http://json-schema.org/draft-03/schema\",\n \"properties\": {\n \"input\": {\n \"required\": false,\n \"type\": \"string\"\n }\n },\n \"required\": false,\n \"type\": \"object\"\n}\n"
},
{
"description": null,
"example": null,
"formParameters": [],
"name": "application/vnd.external+json",
"schema": {
"value": "External"
},
"schemaContent": "{\n\t\"$schema\": \"http://json-schema.org/draft-04/schema\",\n\t\"type\": \"object\",\n\t\"properties\": {\n\t\t\"stringProperty\": {\n\t\t\t\"type\": \"string\"\n\t\t}\n\t},\n\t\"required\": [\n\t\t\"stringProperty\"\n\t]\n}"
}
],
"code": {
Expand Down Expand Up @@ -399,6 +419,12 @@
"value": {
"value": "<xs:schema attributeFormDefault=\"unqualified\"\n elementFormDefault=\"qualified\"\n xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n <xs:element name=\"api-request\">\n <xs:complexType>\n <xs:sequence>\n <xs:element type=\"xs:string\" name=\"input\"/>\n </xs:sequence>\n </xs:complexType>\n </xs:element>\n</xs:schema>\n"
}
},
{
"key": "External",
"value": {
"value": "{\n\t\"$schema\": \"http://json-schema.org/draft-04/schema\",\n\t\"type\": \"object\",\n\t\"properties\": {\n\t\t\"stringProperty\": {\n\t\t\t\"type\": \"string\"\n\t\t}\n\t},\n\t\"required\": [\n\t\t\"stringProperty\"\n\t]\n}"
}
}
],
"securedBy": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,13 @@ public void setContextNode(Node node)
{
this.contextNode = node;
}

@Override
public String getPath()
{
final Node parent = getParent();
if (parent == null)
return "";
return parent.getPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,15 @@ public NodeType getType()
{
return NodeType.KeyValue;
}

@Override
public String getPath()
{
final String delimiter;
if (getRootNode() == getParent())
delimiter = "";
else
delimiter = "/";
return super.getPath() + delimiter + String.valueOf(getKey()).replace("/", "\\/");
}
}
5 changes: 5 additions & 0 deletions yagi/src/main/java/org/raml/yagi/framework/nodes/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,9 @@ public interface Node
void setContextNode(Node node);

Node getContextNode();

/**
* Get the path within the raml document.
*/
String getPath();
}