-
Notifications
You must be signed in to change notification settings - Fork 206
WIP json:api helpers #240
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
Closed
Closed
WIP json:api helpers #240
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3613132
Formatting fixes
moxley 213f60c
Basics of `%JsonApiResource{}` `resource_schema/1` and `resource_attr…
moxley 8582872
Generate document schema
moxley bf367f3
Use `%JsonApiDocument{}`
moxley e162cd4
Represent document as a single resource or list of resources
moxley 8550e77
Refactor
moxley bf62a47
wip test JasonApiHelpers integration with operation specs
moxley abdcf48
Working example
moxley 7814f18
wip
moxley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| defmodule OpenApiSpex.JsonApiHelpers do | ||
| alias OpenApiSpex.JsonApiHelpers.{JsonApiDocument, JsonApiResource} | ||
|
|
||
| def document_schema(document) do | ||
| JsonApiDocument.schema(document) | ||
| end | ||
|
|
||
| def resource_schema(resource) do | ||
| JsonApiResource.schema(resource) | ||
| end | ||
|
|
||
| defmacro generate_document_schema(attrs) do | ||
| quote do | ||
| require OpenApiSpex | ||
|
|
||
| @document struct!(JsonApiDocument, unquote(attrs)) | ||
| def document, do: @document | ||
|
|
||
| @document_schema JsonApiDocument.schema(@document) | ||
| def document_schema, do: @document_schema | ||
|
|
||
| OpenApiSpex.schema(@document_schema) | ||
| end | ||
| end | ||
|
|
||
| defmacro generate_resource_schema(attrs) do | ||
| quote do | ||
| require OpenApiSpex | ||
|
|
||
| @resource struct!(JsonApiResource, unquote(attrs)) | ||
| def resource, do: @resource | ||
|
|
||
| @resource_schema JsonApiResource.schema(@resource) | ||
| def resource_schema, do: @resource_schema | ||
|
|
||
| OpenApiSpex.schema(@resource_schema) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| defmodule OpenApiSpex.JsonApiHelpers.JsonApiDocument do | ||
| alias OpenApiSpex.Schema | ||
| alias OpenApiSpex.JsonApiHelpers.JsonApiResource | ||
|
|
||
| defstruct resource: nil, | ||
| multiple: false, | ||
| title: nil, | ||
| "x-struct": nil | ||
|
|
||
| def schema(%__MODULE__{} = document) do | ||
| if not is_binary(document.title) do | ||
| raise "%JsonApiDocument{} :title is required and must be a string" | ||
| end | ||
|
|
||
| resource = document.resource | ||
| resource_item_schema = JsonApiResource.schema(resource) | ||
|
|
||
| resource_title = | ||
| case resource_item_schema do | ||
| %Schema{} = schema -> schema.title | ||
| module when is_atom(module) and not is_nil(module) -> module.schema().title | ||
| end | ||
|
|
||
| resource_schema = | ||
| if document.multiple do | ||
| %Schema{ | ||
| type: :array, | ||
| items: resource_item_schema, | ||
| title: resource_title <> "List" | ||
| } | ||
| else | ||
| resource_item_schema | ||
| end | ||
|
|
||
| %Schema{ | ||
| type: :object, | ||
| properties: %{ | ||
| data: resource_schema | ||
| }, | ||
| required: [:data], | ||
| title: document.title, | ||
| "x-struct": document."x-struct" | ||
| } | ||
| end | ||
|
|
||
| def schema(document_attrs) when is_list(document_attrs) or is_map(document_attrs) do | ||
| __MODULE__ | ||
| |> struct!(document_attrs) | ||
| |> schema() | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| defmodule OpenApiSpex.JsonApiHelpers.JsonApiResource do | ||
| alias OpenApiSpex.Schema | ||
|
|
||
| defstruct additionalProperties: nil, | ||
| properties: %{}, | ||
| required: [], | ||
| title: nil | ||
|
|
||
| def schema(resource) when is_atom(resource) and not is_nil(resource) do | ||
| resource.schema()."x-struct" | ||
| end | ||
|
|
||
| def schema(%__MODULE__{} = resource) do | ||
| if not is_binary(resource.title) do | ||
| raise "%JsonApiResource{} :title is required and must be a string" | ||
| end | ||
|
|
||
| %Schema{ | ||
| type: :object, | ||
| properties: %{ | ||
| id: %Schema{type: :string}, | ||
| type: %Schema{type: :string}, | ||
| attributes: attributes_schema(resource) | ||
| }, | ||
| required: [:id, :type], | ||
| title: resource.title <> "Resource" | ||
| } | ||
| end | ||
|
|
||
| def attributes_schema(%__MODULE__{} = resource) do | ||
| if not is_binary(resource.title) do | ||
| raise "%JsonApiResource{} :title is required and must be a string" | ||
| end | ||
|
|
||
| %Schema{ | ||
| type: :object, | ||
| properties: resource.properties, | ||
| title: resource.title <> "Attributes" | ||
| } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| defmodule OpenApiSpex.JsonApiHelpersTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias OpenApiSpexTest.{CartDocument, CartIndexDocument, CartResource} | ||
| alias OpenApiSpex.{JsonApiHelpers, Schema} | ||
| alias OpenApiSpex.JsonApiHelpers.JsonApiResource | ||
|
|
||
| describe "from operation specs" do | ||
| test "index action" do | ||
| spec = OpenApiSpexTest.ApiSpec2.spec() | ||
| assert %Schema{} = _schema = spec.components.schemas["CartIndexResponse"] | ||
| end | ||
| end | ||
|
|
||
| describe "generate_resource_document/1" do | ||
| test "generate schema/0" do | ||
| assert %Schema{} = schema = CartDocument.schema() | ||
| assert schema.title == "CartDocument" | ||
| assert %{data: _} = schema.properties | ||
| assert schema.properties.data.title == CartResource.schema().title | ||
| end | ||
|
|
||
| test "generate schema for index document" do | ||
| assert %Schema{} = schema = CartIndexDocument.schema() | ||
| assert schema.title == "CartIndexDocument" | ||
| assert %{data: _} = schema.properties | ||
| assert schema.properties.data.type == :array | ||
| assert schema.properties.data.items == OpenApiSpexTest.CartResource | ||
| end | ||
| end | ||
|
|
||
| describe "generate_resource_schema/1" do | ||
| test "generate resource/0 and resource_schema/0" do | ||
| assert %JsonApiResource{} = CartResource.resource() | ||
| assert %Schema{} = schema = CartResource.schema() | ||
| assert schema.title == "CartResource" | ||
| end | ||
| end | ||
|
|
||
| describe "resource_schema/1" do | ||
| test "attributes" do | ||
| resource = CartResource.resource() | ||
| schema = JsonApiHelpers.resource_schema(resource) | ||
| assert schema.properties.attributes == JsonApiResource.attributes_schema(resource) | ||
| assert %Schema{} = schema.properties.id | ||
| assert %Schema{} = schema.properties.type | ||
| end | ||
|
|
||
| test "title" do | ||
| resource = CartResource.resource() | ||
| schema = JsonApiHelpers.resource_schema(resource) | ||
| assert schema.title == "CartResource" | ||
| end | ||
| end | ||
|
|
||
| describe "attributes_schema/1" do | ||
| test "generates schema with same properties" do | ||
| resource = CartResource.resource() | ||
| schema = JsonApiResource.attributes_schema(resource) | ||
| assert schema.properties == resource.properties | ||
| end | ||
|
|
||
| test "generates title" do | ||
| resource = CartResource.resource() | ||
| schema = JsonApiResource.attributes_schema(resource) | ||
| assert schema.title == "CartAttributes" | ||
| end | ||
|
|
||
| test ":title must be a string" do | ||
| resource = CartResource.resource() | ||
| resource = %{resource | title: nil} | ||
|
|
||
| assert_raise( | ||
| RuntimeError, | ||
| "%JsonApiResource{} :title is required and must be a string", | ||
| fn -> | ||
| JsonApiResource.attributes_schema(resource) | ||
| end | ||
| ) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| defmodule OpenApiSpexTest.ApiSpec2 do | ||
| alias OpenApiSpex.{OpenApi, Contact, License, Paths, Server, Info} | ||
| alias OpenApiSpexTest.Router2 | ||
|
|
||
| @behaviour OpenApi | ||
|
|
||
| @impl OpenApi | ||
| def spec() do | ||
| %OpenApi{ | ||
| servers: [ | ||
| %Server{url: "http://example.com"} | ||
| ], | ||
| info: %Info{ | ||
| title: "A", | ||
| version: "3.0", | ||
| contact: %Contact{ | ||
| name: "joe", | ||
| email: "[email protected]", | ||
| url: "https://help.joe.com" | ||
| }, | ||
| license: %License{ | ||
| name: "MIT", | ||
| url: "http://mit.edu/license" | ||
| } | ||
| }, | ||
| paths: Paths.from_router(Router2) | ||
| } | ||
| |> OpenApiSpex.resolve_schema_modules() | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| defmodule OpenApiSpexTest.CartDocument do | ||
| alias OpenApiSpex.JsonApiHelpers | ||
| alias OpenApiSpexTest.CartResource | ||
|
|
||
| require OpenApiSpex.JsonApiHelpers | ||
|
|
||
| JsonApiHelpers.generate_document_schema( | ||
| title: "CartDocument", | ||
| resource: CartResource.resource() | ||
| ) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| defmodule OpenApiSpexTest.CartIndexDocument do | ||
| alias OpenApiSpex.JsonApiHelpers | ||
| alias OpenApiSpexTest.CartResource | ||
|
|
||
| require OpenApiSpex.JsonApiHelpers | ||
|
|
||
| JsonApiHelpers.generate_document_schema( | ||
| title: "CartIndexDocument", | ||
| multiple: true, | ||
| resource: CartResource | ||
| ) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| defmodule OpenApiSpexTest.CartResource do | ||
| alias OpenApiSpex.JsonApiHelpers | ||
| alias OpenApiSpex.Schema | ||
|
|
||
| require OpenApiSpex.JsonApiHelpers | ||
|
|
||
| JsonApiHelpers.generate_resource_schema( | ||
| title: "Cart", | ||
| properties: %{ | ||
| total: %Schema{type: :integer} | ||
| }, | ||
| additionalProperties: false | ||
| ) | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.