Closed thbar closed 1 year ago
Another hint: if the JSON contains siren
key, and I set siren
with nullable: false
in the definition (or remove that flag completely, to get the default behaviour), then assert_schema
will warn me with Value does not conform to schema AOM: null value where string expected at /siren
.
But extraneous or missing properties seem to not result into errors (I am trying to use assert_schema
to detect out-of-sync specifications).
I wrote a reproduction using test_assertions_test.exs
in this repo:
test "my stuff" do
schema = %Schema{
type: :object,
properties: %{
hello: %Schema{type: :string, nullable: false}
}
}
api_spec = ApiSpec.empty_spec()
api_spec = put_in(api_spec.components.schemas, %{schema.title => schema})
# does not raise! How can we get an error here, since "incorrect" is not in the schema?
TestAssertions.assert_response_schema(%{incorrect: "key"}, schema.title, api_spec)
# also, "hello" is not nullable above, how can we get an error to warn us that the key also missing?
end
You have to use required
and additionalProperties
:
OpenApiSpex.schema(%{
title: "AOM",
description: "AOM object",
type: :object,
required: [
:name
],
properties: %{
siren: %Schema{type: :string, nullable: true},
name: %Schema{type: :string}
},
additionalProperties: false
})
Great, thank you! I will try that.
I seem to believe that:
required: ...
tells that the keys are mandatory (but does not apply constraint to the values)nullable: false
is the default, but one can make the value optional with nullable: true
additionalProperties: false
is the most important bit and helps me detect legacy fields in the spec!More information at:
I could prepare a PR to improve the readme about this, @zorbash what do you think?
Somewhat similar in spirit to:
For the following type of definition:
I was surprised to see that something like this does not raise:
name
is a mandatory key and the response does not contain it. Also the JSON has extraneous keys etc.Am I doing something incorrect here? Thanks!