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

Commit

Permalink
Also enable model extensions in existing keys
Browse files Browse the repository at this point in the history
Use a LinkedHashMap to ensure that fields from model sub-classes are
preferred over fields from base classes.
  • Loading branch information
oberlies committed Nov 13, 2015
1 parent e812601 commit 44df0e4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@
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
Expand All @@ -34,6 +42,15 @@ public void parseExtendedModel()
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;
Expand All @@ -49,4 +66,36 @@ 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;
}
}
}
7 changes: 7 additions & 0 deletions src/test/resources/org/raml/parser/visitor/extended.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#%RAML 0.8
title: extended model
extension: additional data

securitySchemes:
- extended:
description: Security schema documentation
extension:
key1: foo
key2: bar

0 comments on commit 44df0e4

Please sign in to comment.