-
Notifications
You must be signed in to change notification settings - Fork 0
Document Ecto jsonb usage, fix two codec spec gaps #40
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e853b1
Document and test JSONB column support
andreashasse 4a0ad38
Address review: drop codec example, fix codec ref and schema specs
andreashasse 6c95cb0
Address review: trim the Ecto section, drop the jsonb test module
andreashasse c7bafb0
Fix stale and broken README examples found in review
andreashasse f711f00
Merge remote-tracking branch 'origin/main' into claude/spectral-ecto-…
claude 5cc4c4b
Merge remote-tracking branch 'origin/main' into claude/spectral-ecto-…
claude 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
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
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,62 @@ | ||
| defmodule SpectralCodecHelpersTest do | ||
| @moduledoc """ | ||
| Covers two things a codec author has no way to discover from the outside: that the | ||
| recursive helpers accept a type *reference*, and that `schema/5` may decline a type. | ||
| """ | ||
| use ExUnit.Case, async: true | ||
|
|
||
| describe "recursive helpers accept a type reference" do | ||
| # `Spectral.sp_type_or_ref()` includes `{:type, name, arity}`, so a codec may pass one | ||
| # instead of a resolved node from `Spectral.Type.type_args/1`. | ||
|
|
||
| test "decode/5 resolves the reference against the given type_info" do | ||
| assert {:ok, %CodecRefModule.Inner{name: "Alice"}} = | ||
| Spectral.decode(%{"name" => "Alice"}, CodecRefModule, :outer, :json, [ | ||
| :pre_decoded | ||
| ]) | ||
| end | ||
|
|
||
| test "encode/5 resolves the reference against the given type_info" do | ||
| assert {:ok, %{"name" => "Alice"}} = | ||
| Spectral.encode( | ||
| %CodecRefModule.Inner{name: "Alice"}, | ||
| CodecRefModule, | ||
| :outer, | ||
| :json, | ||
| [ | ||
| :pre_encoded | ||
| ] | ||
| ) | ||
| end | ||
|
|
||
| test "schema/4 resolves the reference against the given type_info" do | ||
| assert %{type: "object", properties: %{"name" => %{type: "string"}}} = | ||
| Spectral.schema(CodecRefModule, :outer, :json_schema, [:pre_encoded]) | ||
| end | ||
|
|
||
| test "schema/4 also resolves a {:record, name} reference" do | ||
| assert %{type: "object"} = | ||
| Spectral.schema(CodecRefModule, :record_ref, :json_schema, [:pre_encoded]) | ||
| end | ||
|
|
||
| test "errors from the resolved type still surface" do | ||
| assert {:error, [%Spectral.Error{} | _]} = | ||
| Spectral.decode(%{"name" => 42}, CodecRefModule, :outer, :json, [:pre_decoded]) | ||
| end | ||
| end | ||
|
|
||
| describe "schema/5 may decline a type" do | ||
| # Once `schema/5` is implemented it receives every type in the codec module, including | ||
| # the ones the codec does not own. | ||
|
|
||
| test "a :continue return falls through to the structural schema" do | ||
| assert %{type: "integer"} = | ||
| Spectral.schema(CodecRefModule, :plain, :json_schema, [:pre_encoded]) | ||
| end | ||
|
|
||
| test "the declined type still encodes and decodes structurally" do | ||
| assert {:ok, 42} = Spectral.decode(42, CodecRefModule, :plain, :json, [:pre_decoded]) | ||
| assert {:ok, 42} = Spectral.encode(42, CodecRefModule, :plain, :json, [:pre_encoded]) | ||
| 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,57 @@ | ||
| defmodule CodecRefModule do | ||
| @moduledoc """ | ||
| A codec that drives every recursive call with a `{:type, name, arity}` reference | ||
| rather than a resolved type node, and whose `schema/5` declines the types it does | ||
| not own. | ||
| """ | ||
| use Spectral.Codec | ||
| use Spectral | ||
|
|
||
| defmodule Inner do | ||
| @moduledoc false | ||
| use Spectral | ||
|
|
||
| defstruct [:name] | ||
|
|
||
| @type t :: %Inner{name: String.t()} | ||
| end | ||
|
|
||
| @type outer :: Inner.t() | ||
|
|
||
| # Not handled by the codec. Every callback must decline it. | ||
| @type plain :: integer() | ||
|
|
||
| # A `{:record, name}` reference is the other half of `sp_type_reference()`. | ||
| @type record_ref :: term() | ||
|
|
||
| @impl Spectral.Codec | ||
| def encode(format, _caller_type_info, {:type, :outer, 0}, _target_type, data, config) do | ||
| Spectral.Codec.encode(format, Inner.__spectra_type_info__(), {:type, :t, 0}, data, config) | ||
| end | ||
|
|
||
| def encode(_format, _caller_type_info, _type_ref, _target_type, _data, _config), do: :continue | ||
|
|
||
| @impl Spectral.Codec | ||
| def decode(format, _caller_type_info, {:type, :outer, 0}, _target_type, input, config) do | ||
| Spectral.Codec.decode(format, Inner.__spectra_type_info__(), {:type, :t, 0}, input, config) | ||
| end | ||
|
|
||
| def decode(_format, _caller_type_info, _type_ref, _target_type, _input, _config), do: :continue | ||
|
|
||
| @impl Spectral.Codec | ||
| def schema(:json_schema, _caller_type_info, {:type, :outer, 0}, _target_type, config) do | ||
| Spectral.Codec.schema(:json_schema, Inner.__spectra_type_info__(), {:type, :t, 0}, config) | ||
| end | ||
|
|
||
| def schema(:json_schema, _caller_type_info, {:type, :record_ref, 0}, _target_type, config) do | ||
| # Built here rather than taken from a module because this project has no Erlang | ||
| # source, so no compiled module carries a record for the helper to look up. | ||
| type_info = | ||
| Spectral.TypeInfo.new(:nomodule, false) | ||
| |> Spectral.TypeInfo.add_record(:point, {:sp_rec, :point, [], 1, %{}}) | ||
|
|
||
| Spectral.Codec.schema(:json_schema, type_info, {:record, :point}, config) | ||
| end | ||
|
|
||
| def schema(_format, _caller_type_info, _type_ref, _target_type, _config), do: :continue | ||
| 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.