Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.
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 @@ -65,7 +65,7 @@ public NodeBuilder getBuilderForTuple(NodeTuple tuple)
return tupleBuilder;
}
}
throw new RuntimeException("Builder not found for " + tuple);
return null;
}

protected Map<String, TupleBuilder<?, ?>> getBuilders()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -38,7 +38,7 @@ public class TupleBuilderFactory extends AbstractFactory
public void addBuildersTo(Class<?> pojoClass, TupleBuilder parent)
{
final List<Field> declaredFields = ReflectionUtils.getInheritedFields(pojoClass);
final Map<String, TupleBuilder<?, ?>> innerBuilders = new HashMap<String, TupleBuilder<?, ?>>();
final Map<String, TupleBuilder<?, ?>> innerBuilders = new LinkedHashMap<String, TupleBuilder<?, ?>>();
for (Field declaredField : declaredFields)
{
Scalar scalar = declaredField.getAnnotation(Scalar.class);
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/raml/parser/visitor/RamlDocumentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,19 @@ public RamlDocumentBuilder()
this(new DefaultResourceLoader());
}

public RamlDocumentBuilder(Class<? extends Raml> documentClass)
{
this(documentClass, new DefaultResourceLoader());
}

public RamlDocumentBuilder(ResourceLoader resourceLoader, TagResolver... tagResolvers)
{
super(Raml.class, resourceLoader, defaultResolver(tagResolvers));
this(Raml.class, resourceLoader, tagResolvers);
}

public RamlDocumentBuilder(Class<? extends Raml> documentClass, ResourceLoader resourceLoader, TagResolver... tagResolvers)
{
super(documentClass, resourceLoader, defaultResolver(tagResolvers));
}

private static TagResolver[] defaultResolver(TagResolver[] tagResolvers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
public class YamlDocumentBuilder<T> implements NodeHandler, ContextPathAware
{

private Class<T> documentClass;
private Class<? extends T> documentClass;
private T documentObject;
private Stack<NodeBuilder<?>> builderContext = new Stack<NodeBuilder<?>>();
private Stack<Object> documentContext = new Stack<Object>();
Expand All @@ -65,7 +65,7 @@ public class YamlDocumentBuilder<T> implements NodeHandler, ContextPathAware
private TagResolver[] tagResolvers;
private ContextPath contextPath;

public YamlDocumentBuilder(Class<T> documentClass, ResourceLoader resourceLoader, TagResolver... tagResolvers)
public YamlDocumentBuilder(Class<? extends T> documentClass, ResourceLoader resourceLoader, TagResolver... tagResolvers)
{
this.documentClass = documentClass;
this.resourceLoader = resourceLoader;
Expand Down Expand Up @@ -270,6 +270,10 @@ public boolean onTupleStart(NodeTuple nodeTuple)
if (currentBuilder != null)
{
NodeBuilder<?> builder = currentBuilder.getBuilderForTuple(nodeTuple);
if (builder == null)
{
return false;
}
builderContext.push(builder);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ public boolean onTupleStart(NodeTuple nodeTuple)
{
try
{
builder.onTupleStart(nodeTuple);
boolean found = builder.onTupleStart(nodeTuple);
if (!found)
{
return false;
}
MappingNode mapping = nodeTuple.getValueNode().getNodeId() == NodeId.mapping ? (MappingNode) nodeTuple.getValueNode() : null;
pushNode(nodeTuple.getKeyNode(), mapping);
}
Expand Down
101 changes: 101 additions & 0 deletions src/test/java/org/raml/parser/visitor/RamlDocumentBuilderTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2015 (c) SAP SE
*
* 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 org.raml.parser.visitor;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.raml.model.Raml;
import org.raml.model.SecurityScheme;
import org.raml.parser.annotation.Mapping;
import org.raml.parser.annotation.Scalar;
import org.raml.parser.annotation.Sequence;
import org.raml.parser.builder.AbstractRamlTestCase;

public class RamlDocumentBuilderTestCase extends AbstractRamlTestCase
{

@Test
public void parseExtendedModel()
{
RamlExt raml = (RamlExt) new RamlDocumentBuilder(RamlExt.class).build("org/raml/parser/visitor/extended.yaml");
assertThat(raml.getTitle(), is("extended model")); // standard property
assertThat(raml.getExtension(), is("additional data")); // non-standard property
}

@Test
public void parseModelWithExtensionInExistingKey()
{
RamlExt2 raml = (RamlExt2) new RamlDocumentBuilder(RamlExt2.class).build("org/raml/parser/visitor/extended.yaml");
SecuritySchemeExt scheme = raml.getSecuritySchemesExt().get(0).get("extended");
assertThat(scheme.getDescription(), is(notNullValue()));
assertThat(scheme.getExtension().get("key1"), is("foo"));
}

public static class RamlExt extends Raml
{
private static final long serialVersionUID = 533345138584973337L;

@Scalar
private String extension;

public String getExtension() {
return extension;
}

public void setExtension(String extension) {
this.extension = extension;
}
}

public static class RamlExt2 extends Raml
{
private static final long serialVersionUID = 1451208177799874616L;

@Sequence(alias = "securitySchemes")
private List<Map<String, SecuritySchemeExt>> securitySchemesExt = new ArrayList<Map<String, SecuritySchemeExt>>();

public List<Map<String, SecuritySchemeExt>> getSecuritySchemesExt() {
return securitySchemesExt;
}

public void setSecuritySchemesExt(List<Map<String, SecuritySchemeExt>> securitySchemesExt) {
this.securitySchemesExt = securitySchemesExt;
}
}

public static class SecuritySchemeExt extends SecurityScheme
{
private static final long serialVersionUID = -7059558387326732177L;

@Mapping
private Map<String,String> extension;

public Map<String, String> getExtension() {
return extension;
}

public void setExtension(Map<String, String> extension) {
this.extension = extension;
}
}
}
18 changes: 18 additions & 0 deletions src/test/java/org/raml/validation/ValidationTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.raml.validation;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.containsString;
Expand All @@ -28,11 +29,13 @@

import org.junit.Ignore;
import org.junit.Test;
import org.raml.model.ActionType;
import org.raml.model.Raml;
import org.raml.parser.builder.AbstractRamlTestCase;
import org.raml.parser.rule.ValidationResult;
import org.raml.parser.tagresolver.ContextPath;
import org.raml.parser.visitor.IncludeInfo;
import org.raml.parser.visitor.RamlDocumentBuilder;

public class ValidationTestCase extends AbstractRamlTestCase
{
Expand Down Expand Up @@ -222,6 +225,21 @@ public void circularInclude()
assertThat(includeInfo.getLine() + 1, is(3));
}

@Test
public void unknownKey()
{
String resource = "org/raml/validation/unknown-key.yaml";

// validation reports the unknown key...
List<ValidationResult> validationResults = validateRaml(resource);
assertThat(validationResults.size(), is(1));
assertThat(validationResults.get(0).getMessage(), is("Unknown key: unknown"));

// ... but the parser doesn't choke on it
Raml validContent = new RamlDocumentBuilder().build(resource);
assertThat(validContent.getResource("/partiallyInvalid").getAction(ActionType.POST), is(notNullValue()));
}

@Test
public void badMediaTypeName()
{
Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/org/raml/parser/visitor/extended.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#%RAML 0.8
title: extended model
extension: additional data

securitySchemes:
- extended:
description: Security schema documentation
extension:
key1: foo
key2: bar
7 changes: 7 additions & 0 deletions src/test/resources/org/raml/validation/unknown-key.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#%RAML 0.8
title: unknown key
/partiallyInvalid:
post:
unknown:
body:
application/json: