Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Question: How to traverse all properties of a data type and its sube types recursively from top to bottom #90

Open
gottschd opened this issue Dec 7, 2020 · 0 comments

Comments

@gottschd
Copy link

gottschd commented Dec 7, 2020

Library version used
0.5.0

Language library used with
Java

Describe the issue
I want to fetch all properties of a root data type and all its sub data types recursively from top to bottom. However,
when loading and resolving the root file, cannot traverse to the bottom of the hierarchy.

Assume the following root data type raml file (excerpt):

#%RAML 1.0 DataType
type: object
properties:
  company:
    displayName: my full company data
    type: !include MyCompany-1.0.raml
    required: true
  availableResources:
    type: array
    items: !include generic/MyResource-1.0.raml
    required: false

The MyCompany-1.0.raml (exerpt):

#%RAML 1.0 DataType
type: object
properties: 
  companyIdentification:
    type: !include MyCompanyIdentification-1.0.raml
    required: true
  companyProfile:
    type: !include MyCompanyProfileBase-1.0.raml
    required: true
    properties: 
      branchOffices:
        type: array
        items: !include MyBranch-1.0.raml
        required: false

The generic/MyResource-1.0.raml:

#%RAML 1.0 DataType
type: object
properties:
  tag:
    type: string
    required: false
  uri:
    type: string
    required: false

Code you have issues with
I started with the following code:

    public static void main( String[] args ) throws Exception
    {
        String docPath = "file://./my-raml-folder/datatypes/My-full-company-data-1.0.raml";
        WebApiDataType doc = (WebApiDataType) Raml10.parse(docPath).get();
        NodeShape ns = (NodeShape) doc.encodes();

        printNodeShape(0, ns );

        System.out.println( "Finished" );
    }

    private static void printPropertyShape( int pLvl, PropertyShape pPropertyShape )
    {
        System.out.println( prefix(pLvl) + pPropertyShape.name() ); 
        System.out.println( prefix(pLvl) + pPropertyShape.range().getClass()); 
        if( NodeShape.class.equals( pPropertyShape.range().getClass() ) ) {
            printNodeShape(pLvl, (NodeShape)pPropertyShape.range());
        } else if( ArrayShape.class.equals( pPropertyShape.range().getClass() ) ) {
            printArrayShape(pLvl, (ArrayShape)pPropertyShape.range());
        }
    }

    private static void printArrayShape(int pLvl, ArrayShape pArrayShape) {
        System.out.println( prefix(pLvl) + pArrayShape.items() );
    }

    private static void printNodeShape(int pLvl, NodeShape pNodeShape) {
        System.out.println( prefix(pLvl) + pNodeShape.properties().size() );
        for (PropertyShape ps : pNodeShape.properties()) {
            printPropertyShape( pLvl+1, ps );
        }

    }

    private static String prefix(int pLvl) {
        String result =  "";
        for (int i = 0; i < pLvl; i++) {
            result += "  ";
        }
        return result;
    }

Actual behaviour/output/error
For the root file i can access the property names,, types, etc. after casting the properties to the appropriate shapes.
However a call to properties() on the company's nodeShape returns 0 (zero) properties, so i cannot go further.
The output:

2
  company
  class amf.client.model.domain.NodeShape
  0 <--- here i would expect the properties of the MyCompany datatype
  availableResources
  class amf.client.model.domain.ArrayShape
  NodeShape(NodeShape(amf.core.parser.Fields@23348b5d,amf.core.parser.Annotations@70325e14))
Finished

Thank you in advance for any help/feedback.

Danny

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant