feat(samples): add support for new values of format keyword (#8882)
This change is specific to JSON Schema 2020-12 and OpenAPI 3.1.0. Refs #8577
This commit is contained in:
@@ -23,7 +23,7 @@ settings:
|
||||
rules:
|
||||
semi: [2, never]
|
||||
strict: 0
|
||||
quotes: [2, double, { allowTemplateLiterals: true }]
|
||||
quotes: [2, double, { avoidEscape: true, allowTemplateLiterals: true }]
|
||||
no-unused-vars: 2
|
||||
no-multi-spaces: 1
|
||||
camelcase: 1
|
||||
@@ -37,4 +37,4 @@ rules:
|
||||
react/display-name: 0
|
||||
mocha/no-exclusive-tests: 2
|
||||
import/no-extraneous-dependencies: 2
|
||||
react/jsx-filename-extension: 2
|
||||
react/jsx-filename-extension: 2
|
||||
|
||||
@@ -18,22 +18,39 @@ const generateStringFromRegex = (pattern) => {
|
||||
}
|
||||
}
|
||||
|
||||
/* eslint-disable camelcase */
|
||||
const primitives = {
|
||||
string: (schema) =>
|
||||
schema.pattern ? generateStringFromRegex(schema.pattern) : "string",
|
||||
string_email: () => "user@example.com",
|
||||
"string_date-time": () => new Date().toISOString(),
|
||||
string_date: () => new Date().toISOString().substring(0, 10),
|
||||
string_uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||
"string_idn-email": () => "실례@example.com",
|
||||
string_hostname: () => "example.com",
|
||||
"string_idn-hostname": () => "실례.com",
|
||||
string_ipv4: () => "198.51.100.42",
|
||||
string_ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
|
||||
string_uri: () => "https://example.com/",
|
||||
"string_uri-reference": () => "path/index.html",
|
||||
string_iri: () => "https://실례.com/",
|
||||
"string_iri-reference": () => "path/실례.html",
|
||||
string_uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
||||
"string_uri-template": () => "https://example.com/dictionary/{term:1}/{term}",
|
||||
"string_json-pointer": () => "/a/b/c",
|
||||
"string_relative-json-pointer": () => "1/0",
|
||||
"string_date-time": () => new Date().toISOString(),
|
||||
string_date: () => new Date().toISOString().substring(0, 10),
|
||||
string_time: () => new Date().toISOString().substring(11),
|
||||
string_duration: () => "P3D", // expresses a duration of 3 days
|
||||
string_password: () => "********",
|
||||
number: () => 0,
|
||||
number_float: () => 0.0,
|
||||
number_float: () => 0.1,
|
||||
number_double: () => 0.1,
|
||||
integer: () => 0,
|
||||
integer_int32: () => (2 ** 30) >>> 0,
|
||||
integer_int64: () => 2 ** 53 - 1,
|
||||
boolean: (schema) =>
|
||||
typeof schema.default === "boolean" ? schema.default : true,
|
||||
}
|
||||
/* eslint-enable camelcase */
|
||||
|
||||
const primitive = (schema) => {
|
||||
schema = objectify(schema)
|
||||
|
||||
2497
test/unit/core/plugins/json-schema-2020-12/samples-extensions/fn.js
Normal file
2497
test/unit/core/plugins/json-schema-2020-12/samples-extensions/fn.js
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* @prettier
|
||||
*/
|
||||
import {
|
||||
memoizedSampleFromSchema,
|
||||
memoizedCreateXMLExample,
|
||||
} from "core/plugins/json-schema-2020-12/samples-extensions/fn"
|
||||
import makeGetSampleSchema from "core/plugins/samples/fn/get-sample-schema"
|
||||
import makeGetJsonSampleSchema from "core/plugins/samples/fn/get-json-sample-schema"
|
||||
import makeGetYamlSampleSchema from "core/plugins/samples/fn/get-yaml-sample-schema"
|
||||
import makeGetXmlSampleSchema from "core/plugins/samples/fn/get-xml-sample-schema"
|
||||
|
||||
describe("getSampleSchema", () => {
|
||||
const oriDate = Date
|
||||
const getSystem = () => ({
|
||||
fn: {
|
||||
memoizedSampleFromSchema,
|
||||
memoizedCreateXMLExample,
|
||||
getJsonSampleSchema: makeGetJsonSampleSchema(getSystem),
|
||||
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
|
||||
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
|
||||
},
|
||||
})
|
||||
const getSampleSchema = makeGetSampleSchema(getSystem)
|
||||
|
||||
beforeEach(() => {
|
||||
Date = function () {
|
||||
this.toISOString = function () {
|
||||
return "2018-07-07T07:07:05.189Z"
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
Date = oriDate
|
||||
})
|
||||
|
||||
it("should stringify string values if json content-type", () => {
|
||||
// Given
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "string",
|
||||
format: "date-time",
|
||||
},
|
||||
"text/json"
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(JSON.stringify(new Date().toISOString()))
|
||||
})
|
||||
|
||||
it("should not unnecessarily stringify string values for other content-types", () => {
|
||||
// Given
|
||||
const res = getSampleSchema({
|
||||
type: "string",
|
||||
format: "date-time",
|
||||
})
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(new Date().toISOString())
|
||||
})
|
||||
|
||||
it("should not unnecessarily stringify non-object values", () => {
|
||||
// Given
|
||||
const res = getSampleSchema({
|
||||
type: "number",
|
||||
})
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(0)
|
||||
})
|
||||
|
||||
it("should not unnecessarily stringify non-object values if content-type is json", () => {
|
||||
// Given
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "number",
|
||||
},
|
||||
"application/json"
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(0)
|
||||
})
|
||||
|
||||
it("should stringify object when literal string example is provided if json content-type", () => {
|
||||
// Given
|
||||
const expected = "<MyObject></MyObject>"
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "object",
|
||||
},
|
||||
"text/json",
|
||||
{},
|
||||
expected
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(JSON.stringify(expected))
|
||||
})
|
||||
|
||||
it("should parse valid json literal example if json content-type", () => {
|
||||
// Given
|
||||
const expected = { test: 123 }
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "object",
|
||||
},
|
||||
"text/json",
|
||||
{},
|
||||
JSON.stringify(expected)
|
||||
)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual.test).toEqual(123)
|
||||
})
|
||||
|
||||
it("should handle number example with string schema as string", () => {
|
||||
// Given
|
||||
const expected = 123
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "string",
|
||||
},
|
||||
"text/json",
|
||||
{},
|
||||
expected
|
||||
)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual).toEqual("123")
|
||||
})
|
||||
|
||||
it("should handle number literal example with string schema as string", () => {
|
||||
// Given
|
||||
const expected = "123"
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "string",
|
||||
},
|
||||
"text/json",
|
||||
{},
|
||||
expected
|
||||
)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual).toEqual("123")
|
||||
})
|
||||
|
||||
it("should handle number literal example with number schema as number", () => {
|
||||
// Given
|
||||
const expected = "123"
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "number",
|
||||
},
|
||||
"text/json",
|
||||
{},
|
||||
expected
|
||||
)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual).toEqual(123)
|
||||
})
|
||||
|
||||
it("should return yaml example if yaml is contained in the content-type", () => {
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "object",
|
||||
},
|
||||
"text/yaml",
|
||||
{},
|
||||
{ test: 123 }
|
||||
)
|
||||
|
||||
expect(res).toEqual("test: 123")
|
||||
})
|
||||
|
||||
it("should return yaml example if yml is contained in the content-type", () => {
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "object",
|
||||
},
|
||||
"text/yml",
|
||||
{},
|
||||
{ test: 123 }
|
||||
)
|
||||
|
||||
expect(res).toEqual("test: 123")
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user