Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

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

Closed
amacleay-cohere opened this issue Jul 8, 2020 · 1 comment · Fixed by #281
Closed

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

amacleay-cohere opened this issue Jul 8, 2020 · 1 comment · Fixed by #281

Comments

@amacleay-cohere
Copy link
Contributor

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):

  • OS: darwin
  • Browser n/a
  • Version n/a

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 prettier/prettier#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
Copy link
Contributor Author

Planning on a PR shortly

amacleay-cohere added a commit to amacleay-cohere/restful-react that referenced this issue Jul 8, 2020
fabien0102 pushed a commit that referenced this issue Jul 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant