Skip to content

Commit

Permalink
fix: allow empty content for Response (open-api-spex#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalDolata authored May 20, 2022
1 parent cde91ea commit fa684e9
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/open_api_spex/operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ defmodule OpenApiSpex.Operation do
"""
@spec response(
description :: String.t(),
media_type :: String.t() | %{String.t() => Keyword.t() | MediaType.t()},
schema_ref :: Schema.t() | Reference.t() | module,
media_type :: String.t() | %{String.t() => Keyword.t() | MediaType.t()} | nil,
schema_ref :: Schema.t() | Reference.t() | module | nil,
opts :: keyword
) :: Response.t()
def response(description, media_type, schema_ref, opts \\ []) do
Expand All @@ -158,7 +158,7 @@ defmodule OpenApiSpex.Operation do
%Response{
description: description,
headers: opts[:headers],
content: build_content_map(media_type, content_opts)
content: build_response_content_map(media_type, content_opts)
}
end

Expand Down Expand Up @@ -303,6 +303,11 @@ defmodule OpenApiSpex.Operation do
|> Schema.validate(params, schemas)
end

defp build_response_content_map(nil, _media_type_opts), do: nil

defp build_response_content_map(media_type, media_type_opts),
do: build_content_map(media_type, media_type_opts)

defp build_content_map(media_type, media_type_opts) when is_binary(media_type) do
%{
media_type => struct!(MediaType, media_type_opts)
Expand All @@ -317,6 +322,6 @@ defmodule OpenApiSpex.Operation do
end

defp build_content_map(media_types, _shared_opts) do
raise "Expected string or map for request_body: #{inspect(media_types)}"
raise "Expected string or map as a media type. Got: #{inspect(media_types)}"
end
end

0 comments on commit fa684e9

Please sign in to comment.