raml-js-parser-2
raml-js-parser-2 copied to clipboard
Body type inheritance support
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?
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.
@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()
@fkrauthan You need to call expand() for your Api in order to apply traits and resource types.
@ddenisenko Shouldn't expand also resolve custom types?
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.
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.