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

Commit e812601

Browse files
committed
Allow parsing RAML files with model extensions
Add constructor so that the RamlDocumentBuilder can also parse custom sub-classes of Raml. In this way, the RAML model can be extended.
1 parent 80b1510 commit e812601

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,19 @@ public RamlDocumentBuilder()
4949
this(new DefaultResourceLoader());
5050
}
5151

52+
public RamlDocumentBuilder(Class<? extends Raml> documentClass)
53+
{
54+
this(documentClass, new DefaultResourceLoader());
55+
}
56+
5257
public RamlDocumentBuilder(ResourceLoader resourceLoader, TagResolver... tagResolvers)
5358
{
54-
super(Raml.class, resourceLoader, defaultResolver(tagResolvers));
59+
this(Raml.class, resourceLoader, tagResolvers);
60+
}
61+
62+
public RamlDocumentBuilder(Class<? extends Raml> documentClass, ResourceLoader resourceLoader, TagResolver... tagResolvers)
63+
{
64+
super(documentClass, resourceLoader, defaultResolver(tagResolvers));
5565
}
5666

5767
private static TagResolver[] defaultResolver(TagResolver[] tagResolvers)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
public class YamlDocumentBuilder<T> implements NodeHandler, ContextPathAware
5757
{
5858

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

68-
public YamlDocumentBuilder(Class<T> documentClass, ResourceLoader resourceLoader, TagResolver... tagResolvers)
68+
public YamlDocumentBuilder(Class<? extends T> documentClass, ResourceLoader resourceLoader, TagResolver... tagResolvers)
6969
{
7070
this.documentClass = documentClass;
7171
this.resourceLoader = resourceLoader;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2015 (c) SAP SE
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an
12+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*/
16+
package org.raml.parser.visitor;
17+
18+
import static org.hamcrest.CoreMatchers.is;
19+
import static org.junit.Assert.assertThat;
20+
21+
import org.junit.Test;
22+
import org.raml.model.Raml;
23+
import org.raml.parser.annotation.Scalar;
24+
import org.raml.parser.builder.AbstractRamlTestCase;
25+
26+
public class RamlDocumentBuilderTestCase extends AbstractRamlTestCase
27+
{
28+
29+
@Test
30+
public void parseExtendedModel()
31+
{
32+
RamlExt raml = (RamlExt) new RamlDocumentBuilder(RamlExt.class).build("org/raml/parser/visitor/extended.yaml");
33+
assertThat(raml.getTitle(), is("extended model")); // standard property
34+
assertThat(raml.getExtension(), is("additional data")); // non-standard property
35+
}
36+
37+
public static class RamlExt extends Raml
38+
{
39+
private static final long serialVersionUID = 533345138584973337L;
40+
41+
@Scalar
42+
private String extension;
43+
44+
public String getExtension() {
45+
return extension;
46+
}
47+
48+
public void setExtension(String extension) {
49+
this.extension = extension;
50+
}
51+
}
52+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#%RAML 0.8
2+
title: extended model
3+
extension: additional data

0 commit comments

Comments
 (0)