contiamo / restful-react

A consistent, declarative way of interacting with RESTful backends, featuring code-generation from Swagger and OpenAPI specs 🔥
MIT License
1.87k stars 109 forks source link

Code generation: " <ParseException> Unable to parse. " for complex yaml #280

Closed amacleay-cohere closed 4 years ago

amacleay-cohere commented 4 years ago

Describe the bug yamljs does not know how to parse some valid YAML documents

To Reproduce With this as the input:

openapi: 3.0.2
servers:
  - url: "http://localhost:8080"
    description: local
info:
  title: test
  version: 1.0.0
  description: test
paths:
  ? /reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallylong
  : description: A description
    get:
      responses:
        "200":
          description: A description
      operationId: anOpId
      description: A description

the following will fail:

yarn restful-react import --file openapi.yaml --output output.tsx
<ParseException> Unable to parse. (line 10: '? /reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallylong')

Expected behavior This should succeed. The syntax in the input file is valid under the YAML spec but yamljs fails to parse it. The above input file is equivalent to

openapi: 3.0.2
servers:
  - url: "http://localhost:8080"
    description: local
info:
  title: test
  version: 1.0.0
  description: test
paths:
  /reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallylong:
    description: A description
    get:
      responses:
        "200":
          description: A description
      operationId: anOpId
      description: A description

but uses block mapping.

I believe the fix will just be to switch to a more fully-feature YAML parser. yamljs says development is slowed and recommends the use of js-yaml so it suggests that library ought to be used instead.

Screenshots n/a

Desktop (please complete the following information):

Smartphone (please complete the following information): n/a

Additional context On very long lines, prettier switches to the block mapping style. I ran into this issue after following https://github.com/prettier/prettier/issues/5599

A really simple demo of yamljs struggling with this syntax and js-yaml working:

# cat yaml-issue.js
const jsYaml = require("js-yaml");
const yamlJs = require("yamljs");
const matches = require("fast-deep-equal");
const assert = require("assert");

const traditional = `top:
  child:
    prop1: true
    prop2: false
`;

// https://yaml.org/spec/1.2/spec.html#id2798425
const explicitBlockMapping = `top:
  ? child
  : prop1: true
    prop2: false
`;

assert(
  matches(jsYaml.safeLoad(traditional), jsYaml.safeLoad(explicitBlockMapping)),
  "Traditional matched explicit block mapping"
);

console.log("js-yaml works!");

try {
  assert(
    matches(yamlJs.parse(traditional), yamlJs.parse(explicitBlockMapping)),
    "Traditional matched explicit block mapping"
  );
} catch (e) {
  console.error(`yamljs struggled: ${e}`);
}

Output:

# node yaml-issue.js
js-yaml works!
yamljs struggled: <ParseException> Unable to parse. (line 2: '? child')
# 
amacleay-cohere commented 4 years ago

Planning on a PR shortly