fix: set yaml parser load option to JSON_SCHEMA (#8023)
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import YAML from "js-yaml"
|
import YAML, { JSON_SCHEMA } from "js-yaml"
|
||||||
import { Map } from "immutable"
|
import { Map } from "immutable"
|
||||||
import parseUrl from "url-parse"
|
import parseUrl from "url-parse"
|
||||||
import { serializeError } from "serialize-error"
|
import { serializeError } from "serialize-error"
|
||||||
@@ -62,7 +62,7 @@ export const parseToJson = (str) => ({specActions, specSelectors, errActions}) =
|
|||||||
try {
|
try {
|
||||||
str = str || specStr()
|
str = str || specStr()
|
||||||
errActions.clear({ source: "parser" })
|
errActions.clear({ source: "parser" })
|
||||||
json = YAML.load(str)
|
json = YAML.load(str, { schema: JSON_SCHEMA })
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
// TODO: push error to state
|
// TODO: push error to state
|
||||||
console.error(e)
|
console.error(e)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import cssEscape from "css.escape"
|
|||||||
import getParameterSchema from "../helpers/get-parameter-schema"
|
import getParameterSchema from "../helpers/get-parameter-schema"
|
||||||
import randomBytes from "randombytes"
|
import randomBytes from "randombytes"
|
||||||
import shaJs from "sha.js"
|
import shaJs from "sha.js"
|
||||||
import YAML from "js-yaml"
|
import YAML, { JSON_SCHEMA } from "js-yaml"
|
||||||
|
|
||||||
|
|
||||||
const DEFAULT_RESPONSE_KEY = "default"
|
const DEFAULT_RESPONSE_KEY = "default"
|
||||||
@@ -651,7 +651,7 @@ const getYamlSampleSchema = (schema, config, contentType, exampleOverride) => {
|
|||||||
yamlString = YAML.dump(YAML.load(jsonExample), {
|
yamlString = YAML.dump(YAML.load(jsonExample), {
|
||||||
|
|
||||||
lineWidth: -1 // don't generate line folds
|
lineWidth: -1 // don't generate line folds
|
||||||
})
|
}, { schema: JSON_SCHEMA })
|
||||||
if(yamlString[yamlString.length - 1] === "\n") {
|
if(yamlString[yamlString.length - 1] === "\n") {
|
||||||
yamlString = yamlString.slice(0, yamlString.length - 1)
|
yamlString = yamlString.slice(0, yamlString.length - 1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
openapi: 3.0.0
|
||||||
|
|
||||||
|
info:
|
||||||
|
version: 1.0.0
|
||||||
|
title: Native date formats in YAML
|
||||||
|
|
||||||
|
paths:
|
||||||
|
/foo:
|
||||||
|
get:
|
||||||
|
description: Has date without quotes
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Ok
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
without-quotes:
|
||||||
|
type: string
|
||||||
|
format: date
|
||||||
|
example: 1999-11-31
|
||||||
|
with-quotes:
|
||||||
|
type: string
|
||||||
|
format: date
|
||||||
|
example: "1999-12-31"
|
||||||
23
test/e2e-cypress/tests/features/spec-parse-to-json.js
Normal file
23
test/e2e-cypress/tests/features/spec-parse-to-json.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/**
|
||||||
|
* @prettier
|
||||||
|
*/
|
||||||
|
|
||||||
|
describe("Parse YAML as YAML@1.2 with json_schema for all JSON-supported types", () => {
|
||||||
|
it("should have date string even without quotes", () => {
|
||||||
|
cy.visit("/?url=/documents/features/spec-parse-to-json.yaml")
|
||||||
|
.get("#operations-default-get_foo")
|
||||||
|
.click()
|
||||||
|
// Responses -> example value tab
|
||||||
|
.get(".language-json > :nth-child(3)")
|
||||||
|
.should("have.text", "\"without-quotes\"")
|
||||||
|
.get(".language-json > :nth-child(5)")
|
||||||
|
.should("have.text", "\"1999-11-31\"")
|
||||||
|
// Responses -> schema tab
|
||||||
|
.get(".model-example > .tab > :nth-child(2)")
|
||||||
|
.click()
|
||||||
|
.get(":nth-child(1) > :nth-child(2) > .model > :nth-child(1) > .prop > .property") // first element, without-quotes
|
||||||
|
.should("have.text", "example: 1999-11-31")
|
||||||
|
.get(":nth-child(2) > :nth-child(2) > .model > :nth-child(1) > .prop > .property") // second element, with quotes
|
||||||
|
.should("have.text", "example: 1999-12-31")
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user