Skip to content

Commit

Permalink
chore: fix aggregates in filter schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jan 2, 2024
1 parent e6f5ccf commit 4f448b6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
42 changes: 41 additions & 1 deletion lib/ash_json_api/json_schema/open_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,44 @@ if Code.ensure_loaded?(OpenApiSpex) do
resource
|> Ash.Resource.Info.public_attributes()
|> Enum.concat(Ash.Resource.Info.public_calculations(resource))
|> Enum.concat(Ash.Resource.Info.public_aggregates(resource))
|> Enum.map(fn
%Ash.Resource.Aggregate{} = agg ->
field =
if agg.field do
related = Ash.Resource.Info.related(resource, agg.relationship_path)
Ash.Resource.Info.field(related, agg.field)
end

field_type =
if field do
field.type
end

field_constraints =
if field do
field.constraints
end

{:ok, type, constraints} =
Aggregate.kind_to_type(agg.kind, field_type, field_constraints)

type = Ash.Type.get_type(type)

allow_nil? =
is_nil(Ash.Query.Aggregate.default_value(agg.kind))

%{
name: agg.name,
description: agg.description,
type: type,
constraints: constraints,
allow_nil?: allow_nil?
}

other ->
other
end)
|> Enum.reject(&AshJsonApi.Resource.only_primary_key?(resource, &1.name))
|> Map.new(fn attr ->
{attr.name,
Expand Down Expand Up @@ -731,6 +769,8 @@ if Code.ensure_loaded?(OpenApiSpex) do
{:ok, type, _constraints} =
Aggregate.kind_to_type(agg.kind, field_type, field_constraints)

type = Ash.Type.get_type(type)

{agg.name, attribute_filter_schema(type)}
end)
|> Enum.into(props)
Expand All @@ -743,7 +783,7 @@ if Code.ensure_loaded?(OpenApiSpex) do

@spec relationship_filter_schema(relationship :: Relationships.relationship()) :: Schema.t()
defp relationship_filter_schema(_rel) do
%Schema{type: :string}
%Schema{type: :object, additionalProperties: true}
end

@spec attribute_filter_schema(type :: module) :: Schema.t()
Expand Down
12 changes: 9 additions & 3 deletions test/acceptance/open_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ defmodule Test.Acceptance.OpenApiTest do
calculate(:name_twice, :string, concat([:name, :name], "-"))
end

aggregates do
count(:count_of_tags, :tags)
end

relationships do
belongs_to(:author, Test.Acceptance.OpenApiTest.Author, allow_nil?: false)
has_many(:tags, Test.Acceptance.OpenApiTest.Tag, destination_attribute: :post_id)
Expand Down Expand Up @@ -200,7 +204,7 @@ defmodule Test.Acceptance.OpenApiTest do

assert schema.properties == %{
id: %Schema{type: :object, additionalProperties: true},
author: %Schema{type: :string},
author: %Schema{type: :object, additionalProperties: true},
email: %Schema{type: :object, additionalProperties: true},
hidden: %Schema{
type: :object,
Expand All @@ -212,7 +216,8 @@ defmodule Test.Acceptance.OpenApiTest do
description: "description of attribute :name",
additionalProperties: true
},
tags: %Schema{type: :string}
tags: %Schema{type: :object, additionalProperties: true},
count_of_tags: %Schema{type: :object, additionalProperties: true}
}

assert schema.required == nil
Expand Down Expand Up @@ -333,7 +338,8 @@ defmodule Test.Acceptance.OpenApiTest do
%OpenApiSpex.Schema{type: :string},
%OpenApiSpex.Schema{type: :null}
]
}
},
count_of_tags: %OpenApiSpex.Schema{type: :integer}
},
type: :object
},
Expand Down

0 comments on commit 4f448b6

Please sign in to comment.