Skip to content

Commit

Permalink
Merge pull request #162 from mbta/nullable
Browse files Browse the repository at this point in the history
add nullable as a property
  • Loading branch information
mbuhot authored Feb 13, 2018
2 parents dc481a0 + 0b2638b commit e35c5c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/phoenix_swagger/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ defmodule PhoenixSwagger.Schema do
:properties,
:additionalProperties,
:discriminator,
:example]
:example,
:'x-nullable'
]

@doc """
Construct a new %Schema{} struct using the schema DSL.
Expand Down Expand Up @@ -177,7 +179,9 @@ defmodule PhoenixSwagger.Schema do
end
def property(model = %Schema{type: :object}, name, type = %Schema{}, description, opts) do
{required?, opts} = Keyword.pop(opts, :required)
{nullable?, opts} = Keyword.pop(opts, :nullable)
property_schema = struct!(type, [description: type.description || description] ++ opts)
property_schema = if nullable?, do: %{property_schema | :'x-nullable' => true}, else: property_schema
properties = (model.properties || %{}) |> Map.put(name, property_schema)
model = %{model | properties: properties}
if required?, do: required(model, name), else: model
Expand Down Expand Up @@ -215,7 +219,7 @@ defmodule PhoenixSwagger.Schema do
[do: {:__block__, _, exprs}] -> exprs
[do: expr] -> [expr]
end

body =
exprs
|> Enum.map(fn {name, line, args} -> {:property, line, [name | args]} end)
Expand Down
8 changes: 7 additions & 1 deletion test/path_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ defmodule PhoenixSwagger.PathTest do
properties do
subscribe_to_mailing_list :boolean, "mailing list subscription", default: true
send_special_offers :boolean, "special offers list subscription", default: true
phone_number :string, "specified phone", nullable: true
end
end)
end
Expand Down Expand Up @@ -231,7 +232,12 @@ defmodule PhoenixSwagger.PathTest do
"default" => true,
"description" => "mailing list subscription",
"type" => "boolean"
}
},
"phone_number" => %{
"description" => "specified phone",
"type" => "string",
"x-nullable" => true
},
},
"type" => "object"
}
Expand Down

0 comments on commit e35c5c0

Please sign in to comment.