-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
fix(core):fix missing info of flattened_errors #13998
Conversation
2e900f2
to
76cb75e
Compare
@@ -862,6 +863,9 @@ do | |||
-- } | |||
-- } | |||
-- | |||
if type(err_t[ref.field]) == "string" then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems the error message is always string
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@outsinre maybe nil, or table.
err_t[ref.field]
just like field_err_t
errs[entity] = errs[entity] or {} | ||
errs[entity][i] = uniqueness_error_msg(entity, endpoint_key, key) | ||
local errs_item_name = entity | ||
if parent_entity and tostring(parent_idx) then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If parent_idx
is nil
, then tostring(parent_idx)
is true
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@outsinre
i think tostring(nil)
maybe nil
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The printed nil
is a string, not the Lua nil
.
errs_item_name = parent_entity.."|"..tostring(parent_idx).."|"..entity | ||
end | ||
errs[errs_item_name] = errs[errs_item_name] or {} | ||
errs[errs_item_name][i] = uniqueness_error_msg(entity, endpoint_key, key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The errs_item_name
and entity
on this line refer to the same thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@outsinre no.
entity
is its original name.
errs_item_name
is key name of errs
Co-authored-by: Zachary Hu <[email protected]>
} | ||
}) | ||
|
||
local body = assert.response(res).has.status(400) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 400
error is due to DB-less by default does not support this kind of format. DB-less uses declarative format that needs all entities flattened at top-level.
It expects the snis
not nested within the certificates
.
it("flatten_errors when conflicting inputs", function() | ||
local conflicting_input = { | ||
_format_version = "3.0", | ||
consumers = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm 🤔 I don't know if it's possible to properly flatten errors in this case, because the result we get from the declarative validator is ambiguous. Consider this input:
{
_format_version = "3.0",
consumers = {
{
username = "consumer-01",
basicauth_credentials = {
{ username = "user-01", password = "pwd" },
{ username = "user-11", password = "pwd" },
{ username = "user-02", password = "pwd" },
{ username = "user-11", password = "pwd" },
}
},
{
username = "consumer-02",
basicauth_credentials = {
{ username = "user-99", password = "pwd" },
}
},
{
username = "consumer-03",
basicauth_credentials = {
{ username = "user-01", password = "pwd" },
{ username = "user-33", password = "pwd" },
{ username = "user-02", password = "pwd" },
{ username = "user-33", password = "pwd" },
}
}
}
}
It has the following duplicates:
consumers[1].basicauth_credentials[4]
(user-11
)consumers[3].basicauth_credentials[1]
(user-01
)consumers[3].basicauth_credentials[3]
(user-02
)consumers[3].basicauth_credentials[4]
(user-33
)
The error table we receive from :parse_table()
looks like this:
{
basicauth_credentials = {
[1] = "uniqueness violation: 'basicauth_credentials' entity with username set to 'user-01' already declared",
[3] = "uniqueness violation: 'basicauth_credentials' entity with username set to 'user-02' already declared",
[4] = "uniqueness violation: 'basicauth_credentials' entity with username set to 'user-33' already declared",
},
}
- The declarative code actually overwrote the error in
consumers[1].basicauth_credentials[4]
with the error inconsumers[3].basicauth_credentials[4]
(presumably because they both happened at index4
) - We cannot use this information to determine which consumer each
basicauth_credentials
error corresponds to
In summary, I don't think this can be fixed in the error-flattening code alone. A fix within kong.db.schema.others.declarative_config
must be made first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flrgh
I try to add your inputs to test case, it can get 4 results.
Is this what you need?
There are two errors in this case.
The root cause is that DB-less mode expects flattened declarative format as stated here: #13998 (comment). |
Summary
When a nested configuration error occurs, a more precise and identifiable description is required
When nested configuration conflicted occurs, it returns the desired flatterned error
Please see the test cases for details of the problem
Checklist
changelog/unreleased/kong
orskip-changelog
label added on PR if changelog is unnecessary. README.mdIssue reference
Fix FTI-6351/FTI-6318