Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested object's additionalProperties default values not used #2593

Closed
3 tasks done
jfblain opened this issue Oct 29, 2021 · 3 comments · Fixed by #3435 or #3571
Closed
3 tasks done

Nested object's additionalProperties default values not used #2593

jfblain opened this issue Oct 29, 2021 · 3 comments · Fixed by #3435 or #3571

Comments

@jfblain
Copy link

jfblain commented Oct 29, 2021

Prerequisites

Description

If an object within the "global" form object contains additionalProperties with defaults on some of their fields, they won't be used or showed in the form. Bellow is an example of such a schema in which the defaults that do not appear are mentioned (x2-additionalProperties and x5).

{
  "title": "Missing default showcase",
  "type": "object",
  "properties": {
    "x1": {
      "type": "string",
      "default": "x1 default"
    },
    "x2": {
      "type":"object",
      "properties": {
        "x3": {
          "type": "string",
          "default": "x3 default"
        }
      },
      "additionalProperties": {
        "type": "string",
        "default": "x2-additional default (not showing)"
      }
    }
  },
  "additionalProperties": {
    "type": "object",
    "properties": {
      "x4": {
        "type": "string",
        "default": "x4 default"
      }
    },
    "additionalProperties": {
      "type": "object",
      "properties": {
        "x5": {
          "type": "string",
          "default": "x5 default (not showing)"
        },
        "x6": {
          "type": "array",
          "items": {
            "type": "string",
            "default": "x6 default"
          }
        }
      }
    }
  }
}

Also included in this example schema is that arrays within those "problematic" objects will still use the default values as expected.

Expected behavior

Defaults should always be used as mentioned in the first paragraph of this part of the documentation.

Actual behavior

Defaults are not always present in nested objects' additionalProperties.

Version

3.2.0

@jacqueswho jacqueswho added the bug label Feb 22, 2022
@kovayur
Copy link

kovayur commented Apr 12, 2022

Hi,

Looks like we are also affected by the same issue. Here's the schema example:

{
  "type": "object",
  "default": {
    "resources": {
      "limits": {
        "cpu": "500m",
        "memory": "1Gi"
      },
      "requests": {
        "cpu": "100m",
        "memory": "256Mi"
      }
    }
  },
  "properties": {
    "resources": {
      "type": "object",
      "properties": {
        "limits": {
          "type": "object",
          "additionalProperties": {
            "type": "string"
          }
        },
        "requests": {
          "type": "object",
          "additionalProperties": {
            "type": "string"
          }
        }
      }
    }
  }
}

Expected: Limits and requests are filled with defaults;
Actual: Limits and requests are empty.

Playground link.

heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Feb 6, 2023
Fixes: rjsf-team#2593 by reimplementing rjsf-team#2816. Also reimplemented the empty `anyOf`/`oneOf` handling of rjsf-team#1588

- In `@rjsf/utils`, reimplemented the `additionalProperties` handling logic of rjsf-team#2816 into `computeDefault()`, refactoring some common code into `maybeAddDefaultToObject()`
  - Also deal with empty `anyOf`/`oneOf` arrays by returning undefined, reimplementing rjsf-team#1588
- In `@rjsf/core`, added the `additionalProperties` tests from rjsf-team#2816
- Updated the `CHANGELOG.md` accordingly
heath-freenome added a commit that referenced this issue Feb 6, 2023
* fix: reimplement #1588 and #2816
Fixes: #2593 by reimplementing #2816. Also reimplemented the empty `anyOf`/`oneOf` handling of #1588

- In `@rjsf/utils`, reimplemented the `additionalProperties` handling logic of #2816 into `computeDefault()`, refactoring some common code into `maybeAddDefaultToObject()`
  - Also deal with empty `anyOf`/`oneOf` arrays by returning undefined, reimplementing #1588
- In `@rjsf/core`, added the `additionalProperties` tests from #2816
- Updated the `CHANGELOG.md` accordingly

* - Responded to reviewer feedback
@ezzadeen
Copy link

ezzadeen commented Apr 3, 2023

I still see this problem. Using the Playground:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "test": {
      "title": "Test",
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "properties": {
          "host": {
            "title": "Host",
            "type": "string",
            "default": "localhost"
          },
          "port": {
            "title": "Port",
            "type": "integer",
            "default": 389
          }
        }
      }
    }
  }
}

@heath-freenome
Copy link
Member

Playground link for the above schema example

heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Apr 4, 2023
… addition to data in `default` when dealing with `additionalProperties`
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Apr 4, 2023
… addition to data in `default` when dealing with `additionalProperties`
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Apr 4, 2023
… addition to data in `default` when dealing with `additionalProperties`
heath-freenome added a commit that referenced this issue Apr 4, 2023
* fix: Fixed infinite ref recursion in some utils functions
Fixes #3560 by preventing infinite recursion on `$ref`s
- In `@rjsf/utils` added infinite recursion protection in the `toIdSchema()`, `toPathSchema()` and `getDefaultFormState()` functions
  - Added tests to verify that no infinite recursion due to `$ref`s happen for those three functions along with `retrieveSchema()`
  - Fixed the console.log() call for the `mergeAllOf` exceptions to log the whole error rather than the toString of it
- Updated the `CHANGELOG.md` file accordingly

* - Removed the defaults on the internal functions and in the case of `toIdPrefixInternal()` reordered the props from the public API that are provided with defaults
  - This fixes test coverages

* - Fixed #2593 fully by also supporting data in `formData` in addition to data in `default` when dealing with `additionalProperties`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment