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

Cannot tell namespace from the model

Open vsigler opened this issue 8 years ago • 1 comments

When I import a library in my raml file using the "uses" tag:

#%RAML 1.0 Library
types:
  CommonType:
    type: "object"
    properties:
      name:
        type: "string"

...
uses:
  commonTypes: typeLib.raml
types:
  CommonType:
    type: "object"
  LibExtension:
    type: "commonTypes.CommonType"
...

How can I tell the "local" type from the "library" type if they share the same name? In the RAML, it is evident due to the namespace notation, but when I look at the model obtained from the parser, for LibExtension, I get type "CommonType". But which one is it? Is there something that tells me that this is the one from the library?

I did not find any notion of namespace in the model, so it would seem like a missing feature. I am using version 1.0.8.

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

vsigler avatar May 24 '17 14:05 vsigler

I managed to workaround this by tapping into the underlying Node model and using resource path as namespace identifier. However this falls into the "hack" category and I think it should be much simpler and the namespace should be easily accessible through the RAML model, which should be library-aware.

String getResourcePath(TypeDeclaration typeDeclaration) {
    // in order to get the resource path, it is necessary to tap into the underlying Node model
    Node node = ((NodeModel) typeDeclaration).getNode();
    //fetch the node that contains type reference
    KeyValueNode typeDeclarationNode = node.getChildren().stream()
        .filter(n -> n instanceof KeyValueNode)
        .map(n -> (KeyValueNode) n)
        //it is a child node of type KeyValueNode where key = type
        .filter(n -> (n.getKey() instanceof SYStringNode) && "type".equals(((SYStringNode) n.getKey()).getValue()))
        .findFirst()
        .get();

    NamedTypeExpressionNode typeExpressionNode = (NamedTypeExpressionNode) typeDeclarationNode.getValue();

    //now get the type it actually refers to - this will be the one inside the desired library
    Node reference = typeExpressionNode.resolveReference();

    //once we have the real target node, get its resource path
    return ((SYObjectNode) reference.getSource()).getResourcePath();
  }

vsigler avatar May 25 '17 08:05 vsigler