Skip to content

Regression "oneOf with Date results to null" #330

@mashpie

Description

@mashpie

💥 Regression Report

#325 "oneOf with Date results to null" seams to persist in 2.7.2 and up

Last working version

Worked up to version: 2.7.1

Stopped working in version: 2.7.2

To Reproduce

Steps to reproduce the behavior:

'use strict'

const fastJson = require('fast-json-stringify')
const S = require('fluent-json-schema')

/**
 * @todo works up to [email protected]
 * but broken in [email protected]
 */ 
const isoDateOrEmptyString = S.anyOf([
  S.string().format('date'),
  S.string().maxLength(0).default('')
])

const exampleSchema = S.object()
  .additionalProperties(false)
  .id('exampleSchema')
  .prop('dateIso', isoDateOrEmptyString)
  .prop('dateEmpty', isoDateOrEmptyString)
  .prop('dateNull', isoDateOrEmptyString)
  .valueOf()

const stringify = fastJson(exampleSchema)

console.log(stringify({
  dateIso: '2020-01-01',
  dateEmpty: '',
  dateNull: null,
}))

/**
 * expected
 * @example {"dateIso":"2020-01-01","dateEmpty":"","dateNull":null}
 */

/**
 * reported
 * @example {"dateIso":null,"dateEmpty":"","dateNull":null}
 */

Expected behavior

the snippet should report:

{"dateIso":"2020-01-01","dateEmpty":"","dateNull":null}

Your Environment

  • node version: 14
  • fastify version: 3.15.1 but reproducable without any fastify
  • os: Mac, Linux

Background

I encountered that regression today while upgrading a bunch of services deployed as fastify (v3.7.x - v3.15.1) apps. The JSON returned null for all valid iso formatted dates we had while still handling empty strings correctly. Fortunately we were able to monkey patch dependencies by:

  "resolutions": {
    "fast-json-stringify": "2.7.1"
  }

The same behavior is reproducible without fastify and just plain json-schema. Accordingly, the snippet above fails with a package.json like so:

{
  "name": "tmp-fast-json-stringify",
  "version": "0.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "fast-json-stringify": "^2.7.3",
    "fluent-json-schema": "^3.0.0"
  }
}

but works with

{
  "name": "tmp-fast-json-stringify",
  "version": "0.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "fast-json-stringify": "2.7.1",
    "fluent-json-schema": "^3.0.0"
  }
}

Hope this helps to track down that issue :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions