Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make JsonAPI.resource refuse additionnal attributes #212

Open
phtrivier opened this issue Nov 9, 2018 · 1 comment
Open

Make JsonAPI.resource refuse additionnal attributes #212

phtrivier opened this issue Nov 9, 2018 · 1 comment

Comments

@phtrivier
Copy link

To make a schema refuse additional properties, it's possible to do this:

 foo:
        swagger_schema do
          title("Foo Swagger schema")

          properties do
            bar(:number, "a number", required: true)
          end

          additional_properties false
        end,

This creates a format like:

%{
  "additionalProperties" => false,
  "properties" => %{"bar" => %{"description" => "a number", "type" => "number"}},
  "required" => ["bar"],
  "title" => "Foo Swagger schema",
  "type" => "object"
}

However, I could not find a way to do it for a schema created by JsonApi.resource, is it possible ?

 PhoenixSwagger.JsonApi.resource do
          description("Some JSON API Ressource")

          attributes do
            attribute1(:number, "An attribute", required: true)
          end

          additional_properties false
        end |> IO.inspect

Returns a schema like this, that accepts additional attributes;

%{
  "additionalProperties" => false,
  "description" => "Some JSON API Ressource",
 "properties" => %{
    "attributes" => %{
      "properties" => %{
        "attribute1" => %{"description" => "An attribute", "type" => "number"}
      },
      "required" => ["attribute1"],
      "type" => "object"
    },
    "id" => %{"description" => "The JSON-API resource ID", "type" => "string"},
    "links" => %{"properties" => %{}, "type" => "object"},
    "relationships" => %{"properties" => %{}, "type" => "object"},
    "type" => %{
      "description" => "The JSON-API resource type",
      "type" => "string"
    }
  },
  "type" => "object"
}

Whereas this results in a compilation error:

PhoenixSwagger.JsonApi.resource do
          description("Some JSON API Ressource")

          attributes do
            additional_properties false

            attribute1(:number, "An attribute", required: true)
          end

          additional_properties false
        end |> IO.inspect
@mbuhot
Copy link
Contributor

mbuhot commented Nov 9, 2018

Looks like there isn’t a convenient way to do it without writing a custom function to extend the DSL.

How about adding a new additional_attributes function that puts the additional_properties flag in the attributes schema?

Care to submit a PR for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants