Skip to content

Recursive schema not handled correctly? #627

@jarmo

Description

@jarmo

Hi! Thank you for this library.

I have a problem with describing a recursive schema, which looks something like this in the response JSON:

[
  {
    "name": "foo",
    "children": [
      {
        "name": "bar",
        "children": [
          {
            "name": "baz",
            "children": []
          }
        ]
      }
    ]
  }
]

Basically it represents a tree of nodes of nodes of nodes...

I tried something like this (already figured it out that there's a need to use Reference instead of module itself to avoid compilation errors):

defmodule MyAppWeb.Schemas do
  alias OpenApiSpex.Schema
  alias OpenApiSpex.Reference

  defmodule Node do
    require OpenApiSpex

    OpenApiSpex.schema(%{
      type: :object,
      properties: %{
        name: %Schema{type: :string, description: "Node name"},
        children: %Reference{"$ref": "#/components/schemas/Nodes"}
      },
      required: [:name, :children]
    })
  end

  defmodule Nodes do
    require OpenApiSpex

    OpenApiSpex.schema(%{
      type: :array,
      items: Node,
      minLength: 0
    })
  end
end

This produces an output in the Swagger UI where everything seems to be correct except Node.children is shown as an array of anonymous objects, but nothing about it being array of Node.

How should a relation like this be described so that Swagger UI would at least show the type of array elements for children property?

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