jsonvalidate icon indicating copy to clipboard operation
jsonvalidate copied to clipboard

Serialization using `$ref` returns arrays

Open AlexAxthelm opened this issue 1 year ago • 0 comments

Hi,

I've been working with json_schema$serialise() recently, and I'm seeing that when I attempt to serialise an object using a composed/cobined schema (anything using $ref), the returned values are always arrays.

schema <- '{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "city": { "type": "string" }
    },
    "type": "object",
    "properties": {
        "city": { "$ref": "#/definitions/city" }
    }
}'
                        
validator <- jsonvalidate::json_schema$new(schema)

out <- validator$serialise(list(city = "Firenze"))

print(out)
#> {"city":["Firenze"]}

validator$validate(out, verbose = TRUE)
#> [1] FALSE
#> attr(,"errors")
#>   instancePath              schemaPath keyword   type        message schema
#> 1        /city #/definitions/city/type    type string must be string string
#>     type    data dataPath
#> 1 string Firenze    /city

Created on 2024-01-15 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.2 (2023-10-31)
#>  os       macOS Sonoma 14.2
#>  system   aarch64, darwin23.0.0
#>  ui       unknown
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Belgrade
#>  date     2024-01-15
#>  pandoc   3.1.7 @ /opt/homebrew/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version date (UTC) lib source
#>  cli            3.6.2   2023-12-11 [1] CRAN (R 4.3.1)
#>  curl           5.1.0   2023-10-02 [1] CRAN (R 4.3.1)
#>  digest         0.6.33  2023-07-07 [1] CRAN (R 4.3.1)
#>  evaluate       0.21    2023-05-05 [1] CRAN (R 4.3.1)
#>  fastmap        1.1.1   2023-02-24 [1] CRAN (R 4.3.1)
#>  fs             1.6.3   2023-07-20 [1] CRAN (R 4.3.1)
#>  glue           1.6.2   2022-02-24 [1] CRAN (R 4.3.1)
#>  htmltools      0.5.6   2023-08-10 [1] CRAN (R 4.3.1)
#>  jsonlite       1.8.7   2023-06-29 [1] CRAN (R 4.3.1)
#>  jsonvalidate   1.4.2   2023-11-07 [1] Github (ropensci/jsonvalidate@53600f3)
#>  knitr          1.43    2023-05-25 [1] CRAN (R 4.3.1)
#>  lifecycle      1.0.4   2023-11-07 [1] CRAN (R 4.3.1)
#>  magrittr       2.0.3   2022-03-30 [1] CRAN (R 4.3.1)
#>  purrr          1.0.2   2023-08-10 [1] CRAN (R 4.3.1)
#>  R.cache        0.16.0  2022-07-21 [1] CRAN (R 4.3.1)
#>  R.methodsS3    1.8.2   2022-06-13 [1] CRAN (R 4.3.1)
#>  R.oo           1.25.0  2022-06-12 [1] CRAN (R 4.3.1)
#>  R.utils        2.12.2  2022-11-11 [1] CRAN (R 4.3.1)
#>  R6             2.5.1   2021-08-19 [1] CRAN (R 4.3.1)
#>  Rcpp           1.0.11  2023-07-06 [1] CRAN (R 4.3.1)
#>  reprex         2.0.2   2022-08-17 [1] CRAN (R 4.3.1)
#>  rlang          1.1.2   2023-11-04 [1] CRAN (R 4.3.1)
#>  rmarkdown      2.25    2023-09-18 [1] CRAN (R 4.3.1)
#>  sessioninfo    1.2.2   2021-12-06 [1] CRAN (R 4.3.1)
#>  styler         1.10.2  2023-08-29 [1] CRAN (R 4.3.1)
#>  V8             4.4.0   2023-10-09 [1] CRAN (R 4.3.1)
#>  vctrs          0.6.5   2023-12-01 [1] CRAN (R 4.3.1)
#>  withr          2.5.2   2023-10-30 [1] CRAN (R 4.3.1)
#>  xfun           0.40    2023-08-09 [1] CRAN (R 4.3.1)
#>  yaml           2.3.7   2023-01-23 [1] CRAN (R 4.3.1)
#> 
#>  [1] /opt/homebrew/lib/R/4.3/site-library
#>  [2] /opt/homebrew/Cellar/r/4.3.2/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

The example above is a reprex, but I see the same behavior for definitions defined in separate files ("$ref": "x.json") as well. at a first guess, this seems like the sub-schemas aren't propagating auto_unbox, but I'm out of my depth on JS/V8.

AlexAxthelm avatar Jan 15 '24 17:01 AlexAxthelm