You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pe...@apache.org on 2026/08/10 04:38:40 UTC
(celix) 10/35: Eliminate dead code and redundency.
This is an automated email from the ASF dual-hosted git repository.
PengZheng pushed a commit to branch feature/json-schema
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 3c1fa2b42bbe35a2e568d982c35f31f0e9bfa2d7
Author: PengZheng <ho...@gmail.com>
AuthorDate: Fri Aug 7 13:37:03 2026 +0800
Eliminate dead code and redundency.
---
libs/jansson_ext/gtest/src/test_suite.cpp | 2 --
libs/jansson_ext/gtest/src/test_validate_uri.cpp | 45 ++++++++++++++++++++++++
libs/jansson_ext/src/celix_jansson_schema.c | 41 ++++++---------------
3 files changed, 56 insertions(+), 32 deletions(-)
diff --git a/libs/jansson_ext/gtest/src/test_suite.cpp b/libs/jansson_ext/gtest/src/test_suite.cpp
index 3fe838e67..185e4bca3 100644
--- a/libs/jansson_ext/gtest/src/test_suite.cpp
+++ b/libs/jansson_ext/gtest/src/test_suite.cpp
@@ -47,9 +47,7 @@ static bool is_expected_fail(const std::string& fn) {
/* Format checkers with known limitations */
"email",
"idn-email",
- "bignum",
"ref.json",
- "bignum",
NULL};
for (const char** p = list; *p; p++)
if (fn.find(*p) != std::string::npos)
diff --git a/libs/jansson_ext/gtest/src/test_validate_uri.cpp b/libs/jansson_ext/gtest/src/test_validate_uri.cpp
index b782ff4b0..b641c8c8d 100644
--- a/libs/jansson_ext/gtest/src/test_validate_uri.cpp
+++ b/libs/jansson_ext/gtest/src/test_validate_uri.cpp
@@ -284,6 +284,51 @@ TEST(ValidateUriTest, NonExistentFragmentFallback) {
free_validator(v);
}
+/* ── validate_uri with fragment of an unloaded external document ────────── */
+
+static int unloaded_fragment_loader(const char* /*uri*/, json_t** /*out*/, void* /*ud*/) {
+ return CELIX_JANSSON_SCHEMA_ERROR_LOADER;
+}
+
+TEST(ValidateUriTest, UnloadedExternalFragmentFallback) {
+ /* Root schema references an external document that the loader fails to
+ * load. The file entry exists (document == NULL). validate_uri with a
+ * fragment of that location drives the on-demand document-fragment
+ * resolution, which finds no document and falls back to the root
+ * schema's placeholder $ref. */
+ static const char* schema = R"({
+ "$ref": "http://example.com/schema#/definitions/x"
+ })";
+
+ auto* v = celix_jansson_schema_validator_create(
+ unloaded_fragment_loader, nullptr,
+ celix_jansson_schema_default_format_check, nullptr,
+ nullptr, nullptr);
+ ASSERT_NE(nullptr, v);
+
+ json_t* sch = json_loads(schema, 0, nullptr);
+ ASSERT_NE(nullptr, sch);
+
+ char* errmsg = nullptr;
+ int rc = celix_jansson_schema_set_root_schema(v, sch, &errmsg);
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_LOADER, rc);
+ free(errmsg);
+
+ /* The external document was never loaded, so fragment resolution finds
+ * nothing; the fallback root $ref has no target and reports it. */
+ json_t* inst = json_integer(42);
+ ASSERT_NE(nullptr, inst);
+ reset_errors();
+ EXPECT_EQ(1, celix_jansson_schema_validate_uri(v, inst,
+ "http://example.com/schema#/definitions/x", capture_error, nullptr, nullptr));
+ json_decref(inst);
+ ASSERT_EQ(1u, captured_messages.size());
+ EXPECT_EQ("unresolved or freed schema-reference", captured_messages[0]);
+
+ json_decref(sch);
+ free_validator(v);
+}
+
/* ── validate_uri with $ref-based subschema ────────────────────────────── */
TEST(ValidateUriTest, SubschemaReachedViaRef) {
diff --git a/libs/jansson_ext/src/celix_jansson_schema.c b/libs/jansson_ext/src/celix_jansson_schema.c
index 7bb4ad7cc..795ae402d 100644
--- a/libs/jansson_ext/src/celix_jansson_schema.c
+++ b/libs/jansson_ext/src/celix_jansson_schema.c
@@ -1707,20 +1707,8 @@ static int schema_make_internal_depth(json_t* sch,
celix_jansson_uri_clear(&empty);
}
- if (json_is_true(sch)) {
- celix_jansson_schema_node_t* n = (celix_jansson_schema_node_t*)calloc(1, sizeof(*n));
- if (!n)
- return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
- n->vtable = &vt_boolean;
- n->kind = CELIX_JANSSON_SCHEMA_KIND_BOOLEAN;
- n->root = root;
- n->refcount = 1;
- n->u.boolean.value = true;
- *out = n;
- if (has_id) (void)celix_jansson_schema_root_insert(root, &my_base, n);
- return CELIX_JANSSON_SCHEMA_OK;
- }
- if (json_is_false(sch)) {
+ if (json_is_boolean(sch))
+ {
celix_jansson_schema_node_t* n = (celix_jansson_schema_node_t*)calloc(1, sizeof(*n));
if (!n)
return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
@@ -1728,7 +1716,7 @@ static int schema_make_internal_depth(json_t* sch,
n->kind = CELIX_JANSSON_SCHEMA_KIND_BOOLEAN;
n->root = root;
n->refcount = 1;
- n->u.boolean.value = false;
+ n->u.boolean.value = json_boolean_value(sch);
*out = n;
if (has_id) (void)celix_jansson_schema_root_insert(root, &my_base, n);
return CELIX_JANSSON_SCHEMA_OK;
@@ -1887,8 +1875,7 @@ static celix_jansson_schema_node_t* resolve_document_fragment(
{
celix_jansson_schema_file_t* sf =
(celix_jansson_schema_file_t*)celix_jansson_hash_table_get(&root->files, location);
- if (!sf)
- return NULL;
+ assert(sf != NULL); /* all callers guard on sf first */
json_t* doc = sf->document;
if (!doc && location[0] == '\0')
@@ -2054,27 +2041,21 @@ static int compile_external_document(celix_jansson_schema_root_t* root, const ch
static bool resolve_placeholder(celix_jansson_schema_root_t* root,
const char* location, const char* fragment,
celix_jansson_schema_node_t* ref_node) {
- (void)ref_node;
/* Use the location+fragment to look up in schemas */
celix_jansson_schema_file_t* sf =
(celix_jansson_schema_file_t*)celix_jansson_hash_table_get(&root->files, location);
- if (!sf)
- return false;
+ assert(sf != NULL); /* sole caller resolve_external_refs Phase B already guarantees sf */
/* Already resolved? */
celix_jansson_schema_node_t* existing =
(celix_jansson_schema_node_t*)celix_jansson_hash_table_get(&sf->schemas, fragment);
if (existing) {
- /* Wire up the placeholder if not yet done */
- celix_jansson_schema_node_t* waiting =
- (celix_jansson_schema_node_t*)celix_jansson_hash_table_get(&sf->unresolved, fragment);
- if (waiting && waiting->kind == CELIX_JANSSON_SCHEMA_KIND_REF) {
- waiting->u.ref.target_weak = existing;
- celix_jansson_hash_table_remove(&sf->unresolved, fragment);
- celix_jansson_vec_push(&sf->retained, waiting);
- return true;
- }
- return false;
+ /* ref_node is the REF-kind placeholder the caller fetched from sf->unresolved
+ * (same table and key this function was called with), so wire it directly */
+ ref_node->u.ref.target_weak = existing;
+ celix_jansson_hash_table_remove(&sf->unresolved, fragment);
+ celix_jansson_vec_push(&sf->retained, ref_node);
+ return true;
}
/* Try document fragment walk */