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

References to inner elements instead reference the parent element

Open quilicicf opened this issue 9 years ago • 2 comments

Affects: 1.0.0

Issue

When pointing at a child JSON schema with fragment notation, the parent schema is returned instead.

Spec pointer

Sometimes it is necessary to refer to an element defined in a schema. RAML supports that by using URL fragments as shown in the example below.

type: !include elements.xsd#Foo

How to reproduce

RAML file

#%RAML 1.0
title: API with JSON schema

types:
  InnerElementReference: !include schemas/schema-objectWithComposite.json#simpleComposite

JSON schema file

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "ObjectWithComposite",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "simpleComposite": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "example": "John"
        },
        "arrayComposite": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "example": "Toto"
              }
            }
          }
        }
      }
    }
  },
  "required": [
    "id"
  ]
}

Java file

package com.restlet.definitions.raml10.reader;

import com.fasterxml.jackson.databind.ObjectMapper;
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.JSONTypeDeclaration;

public class Test {

    public static void main(String[] args) throws Exception {
        String savedRamlLocation = Test.class.getResource("filename").toString();
        RamlModelResult ramlModelResult = new RamlModelBuilder().buildApi(savedRamlLocation);
        if (ramlModelResult.hasErrors()) {
            throw new RuntimeException("Parsing of RAML 1.0 file failed with following errors:\n"
                    + new ObjectMapper().writeValueAsString(ramlModelResult.getValidationResults()));
        }

        Api savedRaml = ramlModelResult.getApiV10();
        String innerElementSchemaContent = ((JSONTypeDeclaration) savedRaml.types().get(0)).schemaContent();
    }
}

Output

The string innerElementSchemaContent should contain the schema "simpleComposite" but instead contains "ObjectWithComposite"

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

quilicicf avatar Aug 18 '16 16:08 quilicicf

the fragment identifier indicates which subtype is used for validation but the whole type needs to be included as the subtype may require the whole context to resolve references

svacas avatar Oct 03 '16 18:10 svacas

Right, it is indeed a good idea to keep the whole JSON schema at this level indeed. We'll think of something on our side to retrieve only the interesting part I think. Unless you plan to create a type declaration from the JSON schema in a future version :-p

quilicicf avatar Oct 04 '16 07:10 quilicicf