From 4900271406f5b9027e6135aa157274369fd48ac4 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sat, 24 Aug 2024 22:51:31 +0200 Subject: [PATCH] fix: only add version info to JSON metadata if requested --- src/reader/filesystem_v2.cpp | 19 ++++++++++--------- test/dwarfs_test.cpp | 10 +++++----- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/reader/filesystem_v2.cpp b/src/reader/filesystem_v2.cpp index e2f8e32f1..e3f2bac83 100644 --- a/src/reader/filesystem_v2.cpp +++ b/src/reader/filesystem_v2.cpp @@ -664,15 +664,16 @@ nlohmann::json filesystem_::info_as_json(fsinfo_options const& opts) const { filesystem_parser parser(mm_, image_offset_); - nlohmann::json info{ - {"version", - { - {"major", parser.major_version()}, - {"minor", parser.minor_version()}, - {"header", parser.header_version()}, - }}, - {"image_offset", parser.image_offset()}, - }; + auto info = nlohmann::json::object(); + + if (opts.features.has(fsinfo_feature::version)) { + info["version"] = nlohmann::json::object({ + {"major", parser.major_version()}, + {"minor", parser.minor_version()}, + {"header", parser.header_version()}, + }); + info["image_offset"] = parser.image_offset(); + } if (opts.features.has(fsinfo_feature::history)) { info["history"] = history_.as_json(); diff --git a/test/dwarfs_test.cpp b/test/dwarfs_test.cpp index 119b58e72..6a16b5902 100644 --- a/test/dwarfs_test.cpp +++ b/test/dwarfs_test.cpp @@ -532,12 +532,12 @@ void basic_end_to_end_test( auto info = fs.info_as_json( {.features = reader::fsinfo_features::for_level(detail)}); - ASSERT_TRUE(info.count("version")); - ASSERT_TRUE(info.count("image_offset")); - ASSERT_TRUE(info.count("created_on")); - ASSERT_TRUE(info.count("created_by")); - if (detail >= 1) { + ASSERT_TRUE(info.count("version")); + ASSERT_TRUE(info.count("image_offset")); + ASSERT_TRUE(info.count("created_on")); + ASSERT_TRUE(info.count("created_by")); + ASSERT_TRUE(info.count("block_count")); ASSERT_TRUE(info.count("block_size")); ASSERT_TRUE(info.count("compressed_block_size"));