Nested objects and cyclic dependency in schema

Hello!
I am using the 3.0 version of JsonForms and I am using it on Angular.

My problem is that I have a schema with nested objects and on some cases I have cyclic dependencies of objects, something like this:


{
"definitions": {
      "LocationInput": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "latitude": {
            "type": "string"
          },
          "longitude": {
            "type": "string"
          },
         "factory": {
          "$ref": "#/definitions/LocationInput"
         }
        }
      },
      "FactoryInput": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "location": {
            "$ref": "#/definitions/LocationInput"
          },
        }

"type": "object",
    "properties": {
      "enterprise": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "location": {
            "$ref": "#/definitions/LocationInput"
          },
          "factories": {
            "type": "array",
            "items": {
              "$ref": "#/definitions/FactoryInput"
            }
          }
        }
    }
}

I can “escape” cyclic dependency and nested objects in JsonSchema simply by defining the object in definitions and referencing to it as seen above.
My question is, can I do the same thing in UI Schema? Can I create a field “definitons” for the objects and then reference the objects in UI Schema, if they are a property of other objects?
If not, is there a way doing it? I am a bit new to using JsonForms and I don’t know if there is a way doing it and I couldn’t find any example with nested objects or objects with cyclic dependency.
I tried just giving the object’s path on the schema but the thing is in some cases, I have to use custom renderers for the properties of an object, so default renderered UI schema doesn’t work for me.

It would be really great if we can find a solution for this problem.
Thank you in advance!

Hi @discomel!

The UI Schema does not support dynamic ref resolving, so the issue can’t be solved in the same way.

However, an advanced feature of JSON Forms is passing in a uischemas registry. The registry works in the same manner as the renderers registry: Whenever a sub UI Schema is needed, the uischemas registry is checked whether it contains a fitting one. If yes, then it will be used.

Using this mechanism it’s therefore straightforward to specify UI Schemas which are used throughout a cyclic tree.

Note however that our renderers will by default go as as deeply as needed. So if you don’t have a renderer which is able to stop rendering the ever nested cycle, you will run into an endless rendering loop. An example for an off-the-shelf renderer which will stop is an array renderer, as it will not render the nested UI Schema if there are no elements in the array. If you don’t have an array in your cycle you will need to provide a custom renderer which at some point stops rendering additional UI.

Hey @sdirix! Thank you so much for replying so quick! I will check the solution you mentioned :slight_smile:
However I would like to ask another question based on this exact problem. Can my problem also be solved by using multiple forms? For example for each object the schema contains a new form will be defined. If so, I don’t quite get how we will get the user input and how the user input would look like after the user submits the form.

I hope you can answer this follow-up question too!
Thank you so much!

Hi @discomel,

With a custom renderer you can create an arbitrary architecture as you are in full control of the rendering process. This includes creating multiple nested forms.

However I would advise against that. There is no benefit over having a single form for this use case. On the contrary the glue code between the forms will definitely be much more complicated than handling a single form.

Thank you so much for your quick replies! I will look into the uischemas registry!
Have a nice day :slight_smile: @sdirix