Skip to content

Commit

Permalink
Fix generator default_types relying on map order
Browse files Browse the repository at this point in the history
Fixes #5874.
  • Loading branch information
SteffenDE committed Aug 22, 2024
1 parent bb099f5 commit 62bd7d5
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/mix/phoenix/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ defmodule Mix.Phoenix.Schema do
"""
def value(schema, field, value) do
schema.types
|> Map.fetch!(field)
|> Keyword.fetch!(field)
|> inspect_value(value)
end

Expand Down Expand Up @@ -492,7 +492,7 @@ defmodule Mix.Phoenix.Schema do
end

defp types(attrs) do
Enum.into(attrs, %{}, fn
Keyword.new(attrs, fn
{key, {:enum, vals}} -> {key, {:enum, values: translate_enum_vals(vals)}}
{key, {root, val}} -> {key, {root, schema_type(val)}}
{key, val} -> {key, schema_type(val)}
Expand Down
2 changes: 1 addition & 1 deletion priv/templates/phx.gen.embedded/embedded_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule <%= inspect schema.module %> do
import Ecto.Changeset
alias <%= inspect schema.module %>

embedded_schema do <%= if !Map.equal?(schema.types, %{}) do %>
embedded_schema do <%= if !Enum.empty?(schema.types) do %>
<%= Mix.Phoenix.Schema.format_fields_for_schema(schema) %><% end %>
<%= for {_, k, _, _} <- schema.assocs do %> field <%= inspect k %>, <%= if schema.binary_id do %>:binary_id<% else %>:id<% end %>
<% end %> end
Expand Down
2 changes: 1 addition & 1 deletion test/mix/tasks/phx.gen.embedded_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Mix.Tasks.Phx.Gen.EmbeddedTest do
human_plural: "Nil",
human_singular: "Post",
attrs: [title: :string],
types: %{title: :string},
types: [title: :string],
embedded?: true,
defaults: %{title: ""}
} = schema
Expand Down
4 changes: 2 additions & 2 deletions test/mix/tasks/phx.gen.schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Mix.Tasks.Phx.Gen.SchemaTest do
human_plural: "Posts",
human_singular: "Post",
attrs: [title: :string, tags: :map],
types: %{title: :string, tags: :map},
types: [title: :string, tags: :map],
optionals: [:tags],
route_helper: "post",
defaults: %{title: "", tags: ""},
Expand All @@ -53,7 +53,7 @@ defmodule Mix.Tasks.Phx.Gen.SchemaTest do
human_plural: "Posts",
human_singular: "Post",
attrs: [title: :string],
types: %{title: :string},
types: [title: :string],
route_helper: "api_v1_post",
defaults: %{title: ""},
} = schema
Expand Down

0 comments on commit 62bd7d5

Please sign in to comment.