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

Commit 80b1510

Browse files
committed
Ignore invalid keys in the parser
The validator already detects invalid keys, so the parser doesn't need to report them by throwing an exception. This allows to read the valid parts from an invalid RAML file.
1 parent 6c40f04 commit 80b1510

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

src/main/java/org/raml/parser/builder/DefaultTupleBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public NodeBuilder getBuilderForTuple(NodeTuple tuple)
6565
return tupleBuilder;
6666
}
6767
}
68-
throw new RuntimeException("Builder not found for " + tuple);
68+
return null;
6969
}
7070

7171
protected Map<String, TupleBuilder<?, ?>> getBuilders()

src/main/java/org/raml/parser/visitor/YamlDocumentBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ public boolean onTupleStart(NodeTuple nodeTuple)
270270
if (currentBuilder != null)
271271
{
272272
NodeBuilder<?> builder = currentBuilder.getBuilderForTuple(nodeTuple);
273+
if (builder == null)
274+
{
275+
return false;
276+
}
273277
builderContext.push(builder);
274278
}
275279
else

src/main/java/org/raml/parser/visitor/YamlDocumentSuggester.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ public boolean onTupleStart(NodeTuple nodeTuple)
307307
{
308308
try
309309
{
310-
builder.onTupleStart(nodeTuple);
310+
boolean found = builder.onTupleStart(nodeTuple);
311+
if (!found)
312+
{
313+
return false;
314+
}
311315
MappingNode mapping = nodeTuple.getValueNode().getNodeId() == NodeId.mapping ? (MappingNode) nodeTuple.getValueNode() : null;
312316
pushNode(nodeTuple.getKeyNode(), mapping);
313317
}

src/test/java/org/raml/validation/ValidationTestCase.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.raml.validation;
1717

1818
import static org.hamcrest.CoreMatchers.is;
19+
import static org.hamcrest.CoreMatchers.notNullValue;
1920
import static org.hamcrest.CoreMatchers.nullValue;
2021
import static org.junit.Assert.assertThat;
2122
import static org.junit.matchers.JUnitMatchers.containsString;
@@ -28,11 +29,13 @@
2829

2930
import org.junit.Ignore;
3031
import org.junit.Test;
32+
import org.raml.model.ActionType;
3133
import org.raml.model.Raml;
3234
import org.raml.parser.builder.AbstractRamlTestCase;
3335
import org.raml.parser.rule.ValidationResult;
3436
import org.raml.parser.tagresolver.ContextPath;
3537
import org.raml.parser.visitor.IncludeInfo;
38+
import org.raml.parser.visitor.RamlDocumentBuilder;
3639

3740
public class ValidationTestCase extends AbstractRamlTestCase
3841
{
@@ -222,6 +225,21 @@ public void circularInclude()
222225
assertThat(includeInfo.getLine() + 1, is(3));
223226
}
224227

228+
@Test
229+
public void unknownKey()
230+
{
231+
String resource = "org/raml/validation/unknown-key.yaml";
232+
233+
// validation reports the unknown key...
234+
List<ValidationResult> validationResults = validateRaml(resource);
235+
assertThat(validationResults.size(), is(1));
236+
assertThat(validationResults.get(0).getMessage(), is("Unknown key: unknown"));
237+
238+
// ... but the parser doesn't choke on it
239+
Raml validContent = new RamlDocumentBuilder().build(resource);
240+
assertThat(validContent.getResource("/partiallyInvalid").getAction(ActionType.POST), is(notNullValue()));
241+
}
242+
225243
@Test
226244
public void badMediaTypeName()
227245
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#%RAML 0.8
2+
title: unknown key
3+
/partiallyInvalid:
4+
post:
5+
unknown:
6+
body:
7+
application/json:

0 commit comments

Comments
 (0)