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

Commit

Permalink
Allow parsing RAML files with model extensions
Browse files Browse the repository at this point in the history
Add constructor so that the RamlDocumentBuilder can also parse custom
sub-classes of Raml. In this way, the RAML model can be extended.
  • Loading branch information
oberlies committed Nov 13, 2015
1 parent 80b1510 commit e812601
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.junit.Assert.assertThat;

import org.junit.Test;
import org.raml.model.Raml;
import org.raml.parser.annotation.Scalar;
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
}

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;
}
}
}
3 changes: 3 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,3 @@
#%RAML 0.8
title: extended model
extension: additional data

0 comments on commit e812601

Please sign in to comment.