From 8fd768355e665f54955af4bcda75741ffa1f7d46 Mon Sep 17 00:00:00 2001 From: Steffen Deusch Date: Tue, 27 Aug 2024 16:06:28 +0200 Subject: [PATCH] Fix generator default_types relying on map order (#5907) Fixes #5874. --- lib/mix/phoenix/schema.ex | 4 ++-- priv/templates/phx.gen.embedded/embedded_schema.ex | 2 +- test/mix/tasks/phx.gen.embedded_test.exs | 2 +- test/mix/tasks/phx.gen.schema_test.exs | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/mix/phoenix/schema.ex b/lib/mix/phoenix/schema.ex index 4216d65f50..5f44803acc 100644 --- a/lib/mix/phoenix/schema.ex +++ b/lib/mix/phoenix/schema.ex @@ -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 @@ -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)} diff --git a/priv/templates/phx.gen.embedded/embedded_schema.ex b/priv/templates/phx.gen.embedded/embedded_schema.ex index fe815b932e..81320e36ac 100644 --- a/priv/templates/phx.gen.embedded/embedded_schema.ex +++ b/priv/templates/phx.gen.embedded/embedded_schema.ex @@ -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 diff --git a/test/mix/tasks/phx.gen.embedded_test.exs b/test/mix/tasks/phx.gen.embedded_test.exs index df47f5702a..1bc02c67b4 100644 --- a/test/mix/tasks/phx.gen.embedded_test.exs +++ b/test/mix/tasks/phx.gen.embedded_test.exs @@ -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 diff --git a/test/mix/tasks/phx.gen.schema_test.exs b/test/mix/tasks/phx.gen.schema_test.exs index ea267f9274..e7f62cd9df 100644 --- a/test/mix/tasks/phx.gen.schema_test.exs +++ b/test/mix/tasks/phx.gen.schema_test.exs @@ -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: ""}, @@ -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