How to determine the name of schema types in RAML parser


#1

Hi,

I am using the Java Parser to parse RAML right now. I can get the collection of schemas, their file locations, and am able to pass them to jsonschema2pojo to generate pojo classes that I assume are similar to how the JAX-RS module generates the POJOs.

The part that I am having a hard time with… well two parts really. I am trying to generate SDK classes that are 1 to 1 calls from SDK code to the REST api endpoints. I can get each endpoint path, the method type, the mime types, etc. But when I generate SDK code, I want to include the JSON schema type that is returned as response, or passed in as request body. I am not quite clear how we can determine the name of the json schema generated pojo class in reference to the schema name a given mimetype Schema: element has associated with it. The 2nd problem, which works on the first, is how do I know if the schmea: refers to a schema in the Schemas: section that I include in my RAML file… or if it’s an inlined schema? For example the following RAML snippet:

#%RAML 0.8
title: A API
baseUri: /api/as
version: v1
mediaType: application/json, application/xml
protocols: [ HTTP, HTTPS ]

schemas:

  • as: !include schemas/as.json
    bs: !include schemas/bs.json
    a: !include schemas/a.json
    error: !include schemas/error.json

/as:
displayName: As
post:
body:
application/json:
schema: a
example: |
{ … }
responses:
201:
body:
schema: a
example: |
{ … }
400:
body:
schema: error
get:
responses:
200:
body:
application/json:
schema: as
example: !include example/json/as.json
400:
body:
application/json:
schema: error
example: |
{
“status”: 400
}

In the above, I can parse to the responses, 400, body, schema type. It gives me back the string “error”. Do I then go look up “error” in the ramlObject.schemas list that I get back that I use to generate Pojos from, to make sure it refers to one of those schemas? Or is there some way if I inlined the JSON Schema right in the 400: Schema: line, that it would tell me that it doesn’t refer to a schemas: from the list, but instead is an inlined schema? For that matter I am also unsure if I need to iterate every single one of these schemas: tags in response bodies, etc and if they are inlined schemas, generate POJOs from them as well or what.

Any thoughts/help here would be appreciated.


#2

Hi, you can use the title of the schema as Class Name in Java, and if you schema is in the schema section of your RAML you generate a class, and if it’s a inline you cold generate a innerClass.


#3

@atomswork Yup…that is what I am doing. The bigger issue which I just created a tracking issue for, is how to refer to individual types inside the same schema. In my particular case, it’s not an issue right now, but we do have existing XSDs that have several types defined in them, along with imports/includes that add even more to the namespace. There is no way that I can tell in RAML to pick out a type defined in an XSD (possibly JSON Schema… don’t know that very well), such that if I defined User, Address, Phone in one XSD, I could refer to the schema#type in the schema: RAML element, and the documentation would work as well as the ability to parse this correctly and identify the specific type. When I run an XSD through XJC, several POJOs are created. I don’t know if there is a way yet, to specify those types in RAML. Do you know if there is? I think there isn’t because Christian has asked to track this, but maybe there is some hidden way to do this that doesn’t break the document generators as well?