raml-java-parser icon indicating copy to clipboard operation
raml-java-parser copied to clipboard

The definition of an array type with syntactic sugar '[ ]' is not equivalent to the definition with the 'type' facet.

Open constint opened this issue 9 years ago • 1 comments

Affects 1.0.0

Issue When I use the syntactic sugar [ ], I expect it to be equivalent to using the type facet (as stated in the spec). In both case, the parser returns an ArrayTypeDeclaration, however :

  • When using the type facet (using the example defined below)
    • type() is array
    • items type() is SimpleType
  • When using the square brackets
    • type() is SimpleType[]
    • items type() is {the type of SimpleType}

How to reproduce

#%RAML 1.0
title: My API

types:
  SimpleType:
    properties:
      stringProperty:

  ArrayWithTypeFacet:
    type: array
    items: SimpleType

  ArrayWithSyntacticSugar:
    type: SimpleType[]

Java File

import org.raml.v2.api.RamlModelBuilder;
import org.raml.v2.api.RamlModelResult;
import org.raml.v2.api.model.v10.api.Api;
import org.raml.v2.api.model.v10.datamodel.ArrayTypeDeclaration;
import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertTrue;

public class Test {

    @org.junit.Test
    public void testRaml10Import() throws Exception {

        String savedRamlLocation = getClass().getResource("_api_path_").toString();
        RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(savedRamlLocation);
        Api ramlApi = ramlModelResult.getApiV10();

        TypeDeclaration arrayWithTypeFacet = ramlApi.types().get(1);
        assertTrue(arrayWithTypeFacet instanceof ArrayTypeDeclaration);
        assertThat(arrayWithTypeFacet.name(), is("ArrayWithTypeFacet"));
        assertThat(arrayWithTypeFacet.type(), is("array"));
        assertThat(((ArrayTypeDeclaration) arrayWithTypeFacet).items().type(), is("SimpleType"));

        TypeDeclaration arrayWithSyntacticSugar = ramlApi.types().get(2);
        assertTrue(arrayWithSyntacticSugar instanceof ArrayTypeDeclaration);
        assertThat(arrayWithSyntacticSugar.name(), is("ArrayWithSyntacticSugar"));
        assertThat(arrayWithSyntacticSugar.type(), is("array"));
        assertThat(((ArrayTypeDeclaration) arrayWithSyntacticSugar).items().type(), is("SimpleType"));
    }
}

Aha! Link: https://mulesoft-roadmap.aha.io/features/APIRAML-116

constint avatar Aug 25 '16 14:08 constint

I was going to write an issue about broken array item types when I found this one. The root cause is probably the same. My test case involves some extra indirections and produces weird results:

#%RAML 1.0
title: API with Types
types:
  ObjectList:
    properties:
      list: object[]
  NameList:
    properties:
      list: Name[]
  PersonList:
    properties:
      list: Person[]
  StringList:
    properties:
      list: string[]
  BooleanList:
    properties:
      list: BooleanArray
  DigitList:
    properties:
      list: DigitArray
  BooleanArray:
    type: boolean[]      
  DigitArray:
    type: Digit[]      
  Digit:
    type: integer      
  Name:
    maxLength: 80
  Person:
    properties:
      firstName: string
      lastName: string      
    @Test
    public void shouldFindItemTypeWithBrackets() {
        File input = new File("src/test/resources/raml/bracketArray.raml");
        assertTrue(input.isFile());
        RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(input);
        assertFalse(ramlModelResult.hasErrors());
        Api api = ramlModelResult.getApiV10();
        assertThat(api, is(notNullValue()));

        assertTypes(api.types().get(0), "ObjectList",   "list",         "object[]");
        assertTypes(api.types().get(1), "NameList",     "Name",         "string");
        assertTypes(api.types().get(2), "PersonList",   "Person",       "object");
        assertTypes(api.types().get(3), "StringList",   "list",         "string[]");
        assertTypes(api.types().get(4), "BooleanList",  "BooleanArray", "boolean[]");
        assertTypes(api.types().get(5), "DigitList",    "Digit",        "integer");
    }

    private void assertTypes(TypeDeclaration type, String typeName, String itemName, String itemType) {
        ObjectTypeDeclaration objectType = (ObjectTypeDeclaration) type;
        assertThat(objectType.name(), is(typeName));
        TypeDeclaration member = objectType.properties().get(0);
        assertThat(member, instanceOf(ArrayTypeDeclaration.class));
        ArrayTypeDeclaration arrayType = (ArrayTypeDeclaration) member;
        TypeDeclaration item = arrayType.items();
        assertThat(item.name(), is(itemName));
        assertThat(item.type(), is(itemType));        
    }

The good news is that all item types come out as expected when I replace the square brackets by type: array facets.

hwellmann avatar Sep 07 '16 15:09 hwellmann