raml-org / raml-js-parser

(deprecated) A RAML parser based on PyYAML written in CoffeScript and available for use as NodeJs module or in-browser.
195 stars 53 forks source link

SyntaxError: Unexpected token < in JSON at position 0 #182

Open patrickhousley opened 8 years ago

patrickhousley commented 8 years ago

Looks like dereferenceSchemas is attempting to JSON.parse a raml file.

Below is a redacted version of my raml file.

#%RAML 0.8

---
title: Example API
baseUri: /{version}
version: v1
mediaType: application/json

schemas:
  - cart:  !include schemas/cart/cart.json

resourceTypes:
   - collection: !include resourceTypes/collection.yaml

/carts: !include types/carts.yaml

I changed line 302 of dereferenceSchemas to:

try {
  schema = JSON.parse(schemaNode.value);
} catch (err) {
  console.log(schemaNode.value);
  console.log(schemaNode.start_mark.name);
  throw err;
}

Output:

<<resourcePathName>>
src/resourceTypes/collection.yaml
SyntaxError: Unexpected token < in JSON at position 0
    at Object.parse (native)
    at /Users/patrickhousley/Documents/projects/proj/digital-apis/node_modules/raml-parser/lib/raml.js:303:27
    at Array.map (native)
    at RamlParser.__dirname.RamlParser.RamlParser.dereferenceSchemas (/Users/patrickhousley/Documents/projects/proj/digital-apis/node_modules/raml-parser/lib/raml.js:295:36)
    at RamlParser.dereferenceSchemas (/Users/patrickhousley/Documents/projects/proj/digital-apis/node_modules/raml-parser/lib/raml.js:5:61)
    at /Users/patrickhousley/Documents/projects/proj/digital-apis/node_modules/raml-parser/lib/raml.js:233:24
    at _fulfilled (/Users/patrickhousley/Documents/projects/proj/digital-apis/node_modules/raml-parser/node_modules/q/q.js:798:54)
    at self.promiseDispatch.done (/Users/patrickhousley/Documents/projects/proj/digital-apis/node_modules/raml-parser/node_modules/q/q.js:827:30)
    at Promise.promise.promiseDispatch (/Users/patrickhousley/Documents/projects/proj/digital-apis/node_modules/raml-parser/node_modules/q/q.js:760:13)
    at /Users/patrickhousley/Documents/projects/proj/digital-apis/node_modules/raml-parser/node_modules/q/q.js:574:44

This leads me to believe it is attempting to parse src/resourceTypes/collection.yaml as JSON. Below is the contents of that file:

get:
  description: Get a collection of <<resourcePathName>>.
  responses:
    200:
      body:
        schema: <<resourcePathName>>
        example: <<get-example>>
post:
  description: Creates a <<resourcePathName | !singularize>>.
  body:
      schema: <<resourcePathName | !singularize>>
      example: <<post-example>>
  responses:
      201:
          description: Successfully created the resource.
          headers:
              Location:
                  description: The location of the created resource.
                  type: string
dmartinezg commented 8 years ago

Hi @patrickhousley I cannot seem to be able to reproduce this. How are you testing?

this is the RAML I am using (with you collection.yaml file)

#%RAML 0.8
title: Example API
mediaType: application/json
resourceTypes:
   - collection: !include collection.yaml
/carts:
  type:
    collection:
      get-example: foo
      post-example: bar

maybe there is something weird in your types/carts.yaml file, which is not included in the issue

FredericHeem commented 8 years ago

I was bitten by the same bug so it's a real one.

dmartinezg commented 8 years ago

I don't doubt it, can you share a RAML that I can use? @FredericHeem

patrickhousley commented 8 years ago

Test:

it('should dereference schemas and load the raml object', function (done) {
    raml.loadFile(__dirname + '/../raml-files/dereference-json-schema.raml', { dereferenceSchemas: true }).then(function (ramlObj) {
      done();
    }, function (err) {
      done(err);
    });
  });

dereference-json-schema.raml:

#%RAML 0.8
---
title: example

resourceTypes:
  - collection: !include resourceTypes/collection.yaml

resourceTypes/collection.yaml

get:
  description: Get a collection of <<resourcePathName>>.
  responses:
    200:
      body:
        schema: <<resourcePathName>>
        example: <<get-example>>
post:
  description: Creates a <<resourcePathName | !singularize>>.
  body:
      schema: <<resourcePathName | !singularize>>
      example: <<post-example>>
  responses:
      201:
          description: Successfully created the resource.
          headers:
              Location:
                  description: The location of the created resource.
                  type: string

Result:

patrickhousley@phousley-mac ~/Documents/projects/github/raml-js-parser (master *+)$ npm tst

> raml-parser@0.8.18 test /Users/patrickhousley/Documents/projects/github/raml-js-parser
> grunt test

Running "coffeelint:app" (coffeelint) task
>> 21 files lint free.

Running "coffee:files" (coffee) task
File lib/composer.js created.
File lib/construct.js created.
File lib/errors.js created.
File lib/events.js created.
File lib/joiner.js created.
File lib/loader.js created.
File lib/nodes.js created.
File lib/parser.js created.
File lib/protocols.js created.
File lib/raml.js created.
File lib/reader.js created.
File lib/resolver.js created.
File lib/resourceTypes.js created.
File lib/scanner.js created.
File lib/schemas.js created.
File lib/securitySchemes.js created.
File lib/tokens.js created.
File lib/traits.js created.
File lib/transformations.js created.
File lib/util.js created.
File lib/validator.js created.

Running "connect:server" (connect) task
Started connect web server on 127.0.0.1:9001.

Running "mochacli:all" (mochacli) task
(node:93590) DeprecationWarning: child_process: options.customFds option is deprecated. Use options.stdio instead.

  ․․․․

  3 passing (581ms)
  1 failing

  1) Schemas should dereference schemas and load the raml object:
     SyntaxError: Unexpected token < in JSON at position 0
      at Object.parse (native)
      at /Users/patrickhousley/Documents/projects/github/raml-js-parser/lib/raml.js:302:25
      at Array.map (native)
      at RamlParser.__dirname.RamlParser.RamlParser.dereferenceSchemas (/Users/patrickhousley/Documents/projects/github/raml-js-parser/lib/raml.js:295:36)
      at RamlParser.dereferenceSchemas (/Users/patrickhousley/Documents/projects/github/raml-js-parser/lib/raml.js:5:61)
      at /Users/patrickhousley/Documents/projects/github/raml-js-parser/lib/raml.js:233:24
      at _fulfilled (/Users/patrickhousley/Documents/projects/github/raml-js-parser/node_modules/q/q.js:798:54)
      at self.promiseDispatch.done (/Users/patrickhousley/Documents/projects/github/raml-js-parser/node_modules/q/q.js:827:30)
      at Promise.promise.promiseDispatch (/Users/patrickhousley/Documents/projects/github/raml-js-parser/node_modules/q/q.js:760:13)
      at /Users/patrickhousley/Documents/projects/github/raml-js-parser/node_modules/q/q.js:574:44
      at flush (/Users/patrickhousley/Documents/projects/github/raml-js-parser/node_modules/q/q.js:108:17)
      at _combinedTickCallback (internal/process/next_tick.js:67:7)
      at process._tickCallback (internal/process/next_tick.js:98:9)

Warning:  Use --force to continue.

Aborted due to warnings.
npm ERR! Test failed.  See above for more details.
patrickhousley@phousley-mac ~/Documents/projects/github/raml-js-parser (master *+)$ 
Rotzbua commented 8 years ago

I used https://github.com/raml2html/raml2html which depends on this library. The bug is there also, if you use <<placeholder>> statements. So I think it is an bug of this library. Would be nice if this can be fixed :).