-
Notifications
You must be signed in to change notification settings - Fork 0
Update spectra to 0.14.1: nested doc annotations #39
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
ac4729a
Update spectra to 0.14.0: nested doc annotations
claude a8d1828
Address Copilot review: examples_function and OpenAPI coverage
claude 6c4e892
Merge branch 'main' into andreashasse/awesome-hamilton-ihn49a
claude c2cab7a
Merge branch 'main' into andreashasse/awesome-hamilton-ihn49a
claude 00223cb
Merge branch 'main' into andreashasse/awesome-hamilton-ihn49a
claude 941427e
Update 0.14.0 release date to today
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
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,111 @@ | ||
| defmodule SpectralNestedDocTest do | ||
| # Doc annotations (title, description, deprecated, examples) survive inlining | ||
| # into another schema (spectra 0.14.0). Before 0.14.0 the annotations were | ||
| # kept only on the type that schema generation was entered with. | ||
| use ExUnit.Case, async: true | ||
|
|
||
| defp schema(module, type) do | ||
| module |> Spectral.schema(type) |> IO.iodata_to_binary() |> Jason.decode!() | ||
| end | ||
|
|
||
| defp properties(module, type), do: schema(module, type)["properties"] | ||
|
|
||
| describe "annotations on inlined types" do | ||
| test "a remote annotated type keeps its metadata as a map field value" do | ||
| assert %{ | ||
| "type" => "string", | ||
| "title" => "Payer", | ||
| "description" => "Account charged", | ||
| "deprecated" => true | ||
| } = properties(NestedDocModule, :request)["payer"] | ||
| end | ||
|
|
||
| test "a local annotated type keeps title, description and examples as a map field value" do | ||
| assert %{ | ||
| "type" => "integer", | ||
| "title" => "Amount", | ||
| "description" => "Amount in cents", | ||
| "examples" => [1500] | ||
| } = properties(NestedDocModule, :request)["amount"] | ||
| end | ||
|
|
||
| test "list and non-empty list elements keep their metadata" do | ||
| props = properties(NestedDocModule, :request) | ||
|
|
||
| assert %{"title" => "Tag", "description" => "A short tag"} = props["tags"]["items"] | ||
| assert %{"title" => "Tag", "description" => "A short tag"} = props["more_tags"]["items"] | ||
| assert props["more_tags"]["minItems"] == 1 | ||
| end | ||
|
|
||
| test "a union branch keeps its metadata" do | ||
| assert %{"anyOf" => branches} = properties(NestedDocModule, :request)["note"] | ||
|
|
||
| assert %{"type" => "string", "title" => "Tag", "description" => "A short tag"} in branches | ||
| assert %{"type" => "integer"} in branches | ||
| end | ||
|
|
||
| test "an optional map value keeps its metadata" do | ||
| assert %{"title" => "Tag", "description" => "A short tag"} = | ||
| properties(NestedDocModule, :optional_map)["tag"] | ||
| end | ||
|
|
||
| test "struct fields keep their metadata" do | ||
| props = properties(NestedDocModule, :t) | ||
|
|
||
| assert %{"title" => "Amount", "examples" => [1500]} = props["amount"] | ||
| assert %{"title" => "Tag"} = props["tag"] | ||
| end | ||
| end | ||
|
|
||
| describe "annotation merging through an alias" do | ||
| test "the annotation nearest the use site wins and other keys are kept from both" do | ||
| assert %{"title" => "Label", "description" => "A short tag"} = | ||
| properties(NestedDocModule, :labelled)["label"] | ||
| end | ||
| end | ||
|
|
||
| describe "example validation at inlined positions" do | ||
| test "an example that does not encode as its own type is rejected when inlined" do | ||
| assert_raise ArgumentError, | ||
| ~s{invalid example "not an integer" for type count/0 (schema)}, | ||
| fn -> Spectral.schema(NestedDocBadExampleModule, :wrapper) end | ||
| end | ||
| end | ||
|
|
||
| describe "examples_function at inlined positions" do | ||
| test "the function is called once per position the type is inlined into" do | ||
| before = NestedDocModule.counted_examples_calls() | ||
| props = properties(NestedDocModule, :two_counted) | ||
|
|
||
| assert NestedDocModule.counted_examples_calls() - before == 2 | ||
| assert %{"title" => "Counted", "examples" => [7]} = props["first"] | ||
| assert %{"title" => "Counted", "examples" => [7]} = props["second"] | ||
| end | ||
| end | ||
|
|
||
| describe "annotations in OpenAPI output" do | ||
| test "a response body schema carries the annotations of its nested types" do | ||
| endpoint = | ||
| Spectral.OpenAPI.endpoint(:get, "/payments") | ||
| |> Spectral.OpenAPI.add_response( | ||
| Spectral.OpenAPI.response(200, "OK") | ||
| |> Spectral.OpenAPI.response_with_body(NestedDocModule, {:type, :t, 0}) | ||
| ) | ||
|
|
||
| {:ok, json} = | ||
| Spectral.OpenAPI.endpoints_to_openapi(%{title: "API", version: "1.0"}, [endpoint]) | ||
|
|
||
| spec = json |> IO.iodata_to_binary() |> Jason.decode!() | ||
|
|
||
| assert %{"$ref" => "#/components/schemas/NestedDocModule"} = | ||
| spec["paths"]["/payments"]["get"]["responses"]["200"]["content"][ | ||
| "application/json" | ||
| ]["schema"] | ||
|
|
||
| props = spec["components"]["schemas"]["NestedDocModule"]["properties"] | ||
|
|
||
| assert %{"title" => "Amount", "description" => "Amount in cents"} = props["amount"] | ||
| assert %{"title" => "Tag", "description" => "A short tag"} = props["tag"] | ||
| 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,22 @@ | ||
| defmodule SpectralStructEncodeTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| @moduledoc """ | ||
| Encoding a struct type with data that isn't a map (a string, integer, list, | ||
| or atom) crashed with a raw `badmap` error instead of returning | ||
| `{:error, [%Spectral.Error{}]}`, because the struct branch of spectra's | ||
| encoder was missing the `is_map/1` guard the plain map, list, and record | ||
| branches already had. Fixed in spectra 0.14.1. | ||
| """ | ||
|
|
||
| test "encoding non-map data against a struct type returns a type_mismatch error" do | ||
| assert {:error, [%Spectral.Error{type: :type_mismatch}]} = | ||
| Spectral.encode("not a map", Person, {:type, :t, 0}) | ||
| end | ||
|
|
||
| test "encoding! raises Spectral.Error rather than crashing" do | ||
| assert_raise Spectral.Error, fn -> | ||
| Spectral.encode!("not a map", Person, {:type, :t, 0}) | ||
| 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,11 @@ | ||
| defmodule NestedDocBadExampleModule do | ||
| @moduledoc false | ||
| # An example that does not encode as its own type is rejected at every | ||
| # position the type is inlined into (spectra 0.14.0) | ||
| use Spectral | ||
|
|
||
| spectral(title: "Count", examples: ["not an integer"]) | ||
| @type count :: non_neg_integer() | ||
|
|
||
| @type wrapper :: %{count: count()} | ||
| 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,52 @@ | ||
| defmodule NestedDocModule do | ||
| @moduledoc false | ||
| # Doc annotations propagate into inlined sub-schemas (spectra 0.14.0) | ||
| use Spectral | ||
|
|
||
| defmodule Remote do | ||
| @moduledoc false | ||
| use Spectral | ||
|
|
||
| spectral(title: "Payer", description: "Account charged", deprecated: true) | ||
| @type payer :: String.t() | ||
| end | ||
|
|
||
| spectral(title: "Amount", description: "Amount in cents", examples: [1500]) | ||
| @type amount :: non_neg_integer() | ||
|
|
||
| spectral(title: "Tag", description: "A short tag") | ||
| @type tag :: String.t() | ||
|
|
||
| @type request :: %{ | ||
| payer: Remote.payer(), | ||
| amount: amount(), | ||
| tags: [tag()], | ||
| more_tags: nonempty_list(tag()), | ||
| note: tag() | integer() | ||
| } | ||
|
|
||
| @type optional_map :: %{optional(:tag) => tag()} | ||
|
|
||
| defstruct [:amount, :tag] | ||
|
|
||
| @type t :: %NestedDocModule{amount: amount(), tag: tag()} | ||
|
|
||
| spectral(title: "Label") | ||
| @type label :: tag() | ||
|
|
||
| @type labelled :: %{label: label()} | ||
|
|
||
| spectral(title: "Counted", examples_function: {__MODULE__, :counted_examples, []}) | ||
| @type counted :: non_neg_integer() | ||
|
|
||
| @type two_counted :: %{first: counted(), second: counted()} | ||
|
|
||
| @doc false | ||
| def counted_examples do | ||
| Process.put(:counted_examples_calls, counted_examples_calls() + 1) | ||
| [7] | ||
| end | ||
|
|
||
| @doc false | ||
| def counted_examples_calls, do: Process.get(:counted_examples_calls, 0) | ||
| 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.