Skip to content

Commit fa684e9

Browse files
authored
fix: allow empty content for Response (#453)
1 parent cde91ea commit fa684e9

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

lib/open_api_spex/operation.ex

+9-4
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ defmodule OpenApiSpex.Operation do
139139
"""
140140
@spec response(
141141
description :: String.t(),
142-
media_type :: String.t() | %{String.t() => Keyword.t() | MediaType.t()},
143-
schema_ref :: Schema.t() | Reference.t() | module,
142+
media_type :: String.t() | %{String.t() => Keyword.t() | MediaType.t()} | nil,
143+
schema_ref :: Schema.t() | Reference.t() | module | nil,
144144
opts :: keyword
145145
) :: Response.t()
146146
def response(description, media_type, schema_ref, opts \\ []) do
@@ -158,7 +158,7 @@ defmodule OpenApiSpex.Operation do
158158
%Response{
159159
description: description,
160160
headers: opts[:headers],
161-
content: build_content_map(media_type, content_opts)
161+
content: build_response_content_map(media_type, content_opts)
162162
}
163163
end
164164

@@ -303,6 +303,11 @@ defmodule OpenApiSpex.Operation do
303303
|> Schema.validate(params, schemas)
304304
end
305305

306+
defp build_response_content_map(nil, _media_type_opts), do: nil
307+
308+
defp build_response_content_map(media_type, media_type_opts),
309+
do: build_content_map(media_type, media_type_opts)
310+
306311
defp build_content_map(media_type, media_type_opts) when is_binary(media_type) do
307312
%{
308313
media_type => struct!(MediaType, media_type_opts)
@@ -317,6 +322,6 @@ defmodule OpenApiSpex.Operation do
317322
end
318323

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

0 commit comments

Comments
 (0)