Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions lib/instructor/json_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ defmodule Instructor.JSONSchema do

properties =
ecto_schema.__schema__(:fields)
|> Enum.reject(fn field -> field in ecto_schema.__schema__(:embeds) end)
|> Enum.map(fn field ->
type = ecto_schema.__schema__(:type, field)
value = for_type(type)
Expand Down Expand Up @@ -165,7 +166,29 @@ defmodule Instructor.JSONSchema do
end)
|> Enum.into(%{})

properties = Map.merge(properties, associations)
embeds =
ecto_schema.__schema__(:embeds)
|> Enum.map(&ecto_schema.__schema__(:embed, &1))
|> Enum.map(fn association ->
field = association.field
title = title_for(association.related)

value =
if association.cardinality == :many do
%{
items: %{"$ref": "#/$defs/#{title}"},
title: title,
type: "array"
}
else
%{"$ref": "#/$defs/#{title}"}
end

{field, value}
end)
|> Enum.into(%{})

properties = properties |> Map.merge(associations) |> Map.merge(embeds)
required = Map.keys(properties) |> Enum.sort()
title = title_for(ecto_schema)

Expand Down Expand Up @@ -447,7 +470,8 @@ defmodule Instructor.JSONSchema do
|> maybe_call_with_path(fun, path, opts)
end

defp do_traverse_and_update(tree, fun, path, opts), do: maybe_call_with_path(tree, fun, path, opts)
defp do_traverse_and_update(tree, fun, path, opts),
do: maybe_call_with_path(tree, fun, path, opts)

defp maybe_call_with_path(value, fun, path, opts) do
if Keyword.get(opts, :include_path, false) do
Expand Down
75 changes: 72 additions & 3 deletions test/json_schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@ defmodule JSONSchemaTest do
@primary_key false
embedded_schema do
field(:string, :string)

embeds_many :many, Many do
field(:string, :string)

embeds_one :one, One do
field(:string, :string)

embeds_many :many, Many do
field(:string, :string)
end
end
end
end
end

Expand All @@ -324,19 +336,76 @@ defmodule JSONSchemaTest do
"title" => "string",
"type" => "string",
"description" => "String, e.g. 'hello'"
},
"many" => %{
"items" => %{"$ref" => "#/$defs/JSONSchemaTest.Embedded.Many"},
"title" => "JSONSchemaTest.Embedded.Many",
"type" => "array"
}
},
"required" => ["string"],
"required" => ["many", "string"],
"title" => "JSONSchemaTest.Embedded",
"type" => "object",
"additionalProperties" => false
},
"JSONSchemaTest.Embedded.Many" => %{
"title" => "JSONSchemaTest.Embedded.Many",
"type" => "object",
"required" => ["id", "one", "string"],
"additionalProperties" => false,
"description" => "",
"properties" => %{
"string" => %{
"title" => "string",
"type" => "string",
"description" => "String, e.g. 'hello'"
},
"id" => %{"title" => "id", "type" => "string"},
"one" => %{
"$ref" => "#/$defs/JSONSchemaTest.Embedded.Many.One"
}
}
},
"JSONSchemaTest.Embedded.Many.One" => %{
"title" => "JSONSchemaTest.Embedded.Many.One",
"type" => "object",
"required" => ["id", "many", "string"],
"additionalProperties" => false,
"description" => "",
"properties" => %{
"id" => %{"title" => "id", "type" => "string"},
"string" => %{
"title" => "string",
"type" => "string",
"description" => "String, e.g. 'hello'"
},
"many" => %{
"items" => %{"$ref" => "#/$defs/JSONSchemaTest.Embedded.Many.One.Many"},
"title" => "JSONSchemaTest.Embedded.Many.One.Many",
"type" => "array"
}
}
},
"JSONSchemaTest.Embedded.Many.One.Many" => %{
"title" => "JSONSchemaTest.Embedded.Many.One.Many",
"type" => "object",
"required" => ["id", "string"],
"additionalProperties" => false,
"description" => "",
"properties" => %{
"id" => %{"title" => "id", "type" => "string"},
"string" => %{
"title" => "string",
"type" => "string",
"description" => "String, e.g. 'hello'"
}
}
}
},
"description" => "",
"properties" => %{
"embedded" => %{
"$ref" => "#/$defs/JSONSchemaTest.Embedded",
"title" => "embedded"
"$ref" => "#/$defs/JSONSchemaTest.Embedded"
}
},
"required" => ["embedded"],
Expand Down