raml-js-parser-2 icon indicating copy to clipboard operation
raml-js-parser-2 copied to clipboard

Body type inheritance support

Open dennisfoconnor opened this issue 8 years ago • 6 comments

Currently when parsing a method request body or a response body, it appears that the body object does not inherit properties or examples or from the parent type.

Given the following example:

#%RAML 1.0
title: Pet shop
version: 1
baseUri: /shop

types:
  Pet:
    properties:
      name: string
      kind: string
      price: number
    example:
      name: "Snoopy"
      kind: "Mammal"
      price: 100
/pets:
  /{id}:
    get:
    reponses:
      200:
        body:
          application/json:
            type: Pet

When we run this RAML through through the parser and try to retrieve the body for the response. We get an error.

api.resources()[0].resources()[0].methods()[0].responses()[0].body()[0].properties();
TypeError: api.resources(...)[0].resources(...)[0].methods(...)[0].responses(...)[0].body(...)[0].properties is not a function

Ideally the parser should return the properties and example(s) from the parent type.

Is this feature currently on the roadmap?

dennisfoconnor avatar Jan 05 '16 16:01 dennisfoconnor

Same issue for traits. I use traits for queryParameters() but when I call queryParameters() I just get the ones returned that I define right away for that resource but not the one that are defined in traits.

fkrauthan avatar Jan 05 '16 23:01 fkrauthan

@dennisfoconnor Type declaration AST node itself does not contain parent's properties. Because this node represents what actually is typed by user, the code. What you can do to get parent properties is to get runtime type from the AST node like this:

var bodyType = api.resources()[0].resources()[0].methods()[0].responses()[0].body()[0];

var runtimeBodyType = bodyType.runtimeType();

You get the instance of ITypeDefinition, which can be asked for super types and properties of the super type like this: runtimeBodyType.superTypes()[0].properties()

Or, if you do not want to bother with super type analysis, you can just ask for all type properties, including the ones defined in the super types: runtimeBodyType.allProperties()

ddenisenko avatar Jan 07 '16 16:01 ddenisenko

@fkrauthan You need to call expand() for your Api in order to apply traits and resource types.

ddenisenko avatar Jan 07 '16 16:01 ddenisenko

@ddenisenko Shouldn't expand also resolve custom types?

fkrauthan avatar Jan 08 '16 01:01 fkrauthan

2019, still the same problem.

I have a response body with custom types and I cannot render the properties of these types which makes the parser pretty useless.

DataGreed avatar Jan 11 '19 17:01 DataGreed

Note that raml-js-parser-2 has been deprecated, the new official parser is webapi-parser. Feel free to attempt to reproduce this issue with webapi-parser and report any issue you may have on that repository.

postatum avatar Sep 25 '19 09:09 postatum