raml-org / ramldt2jsonschema

CLI & Library to convert a RAML 1.0 data type to a JSON Schema, and back
Apache License 2.0
59 stars 22 forks source link

resolving references not working #124

Open vanp33 opened 4 years ago

vanp33 commented 4 years ago

Hi, at our project we have a bunch of raml-files, which have references to other ramls. Something like this:

uses:
    market: market_raml_file
type:
    position:
        market:
            type: market.market

I tried the example code but it didn't work. I don't know if there is something need to be configured in order to work? js:

const r2j = require('ramldt2jsonschema')
const join = require('path').join
const fs = require('fs')

const filePath = join(__dirname, 'NC$AFP_TYPE_OBJ_POS.RAML')
const ramlData = fs.readFileSync(filePath).toString()

const basePath = './'

async function main () {
    let schema
    try {
        schema = await r2j.dt2js(ramlData, 'position', { basePath })
    } catch (err) {
        console.log(err)
        return
    }
    console.log(JSON.stringify(schema, null, 2))
}

main()

Best regards

postatum commented 4 years ago

Hi @vanp33.

How exactly didn't it work? Please describe what's the expected and actual output.

Thanks!

vanp33 commented 4 years ago

Hi @postatum , the type, which is referenced by market_raml_file is not resolved. In the json file I got

"market": {
      "description": "A Market"
      // type is missing
} 

It is expected that the type of market is resolved with the schema defined in the market_raml_file

planmillantti commented 4 years ago

References do work with command line, but they do not work in the javascript library.

I have two small RAML files. account.raml:

mediaType: application/json
properties:
  id:
    type: integer
    description: Internal ID of an account

And linktest.raml:

mediaType: application/json

types:
  account: !include account.raml

/accounts:
  get:
    responses:
      200:
        body:                  
          application/json: 
            type: account  

when I run in command line

dt2js linktest.raml account

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/account",
  "definitions": {
    "account": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "description": "Internal ID of an account"
        }
      }
    }
  }
}

This is correct.

However, when I use the following server file:


const r2j = require("ramldt2jsonschema");
const join = require("path").join;
const fs = require("fs");
const currentDir = __dirname;
const ramlData = fs.readFileSync(join(currentDir, 'linktest.raml')).toString()
const main = async function () {
    let schema;
            try {
                let schema = await r2j.dt2js(ramlData, "account");
                console.log(JSON.stringify(schema, null, 2));
                      } catch (e) {
              console.log(e);
              } 
        return;
    };
main();

And I launch from command line

node server.js

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/account",
  "definitions": {
    "account": true
  }
}

The JSON Schema output is broken.

StatusCode404 commented 3 years ago

Any update on this?