Skip to content

! B don't require optional com.google.gson package#769

Draft
JayBazuzi wants to merge 1 commit intomasterfrom
JayBazuzi/optional-gson-1
Draft

! B don't require optional com.google.gson package#769
JayBazuzi wants to merge 1 commit intomasterfrom
JayBazuzi/optional-gson-1

Conversation

@JayBazuzi
Copy link
Contributor

@JayBazuzi JayBazuzi commented Feb 13, 2026

Fixes #768

Summary by Sourcery

Relax JsonApprovals’ dependency on Gson by generalizing its builder callbacks, while keeping existing behavior intact.

Enhancements:

  • Generalize JsonApprovals verifyJson and verifyAsJson overloads to accept a generic Function1 builder callback instead of a GsonBuilder-specific type, decoupling the API from the optional Gson package.

Tests:

  • Adjust JsonApprovals-related tests and the approved documentation snapshot to reflect the new generic builder signatures and updated method references.

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 13, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Generalizes JsonApprovals’ Gson-related overloads to avoid a hard dependency on com.google.gson, while preserving behavior via generics and casts, and updates tests and documentation metadata accordingly.

Class diagram for generalized JsonApprovals Gson overloads

classDiagram

class JsonApprovals {
  +static void verifyJson(String json, boolean reorderJson)
  +static void verifyJson(String json, boolean reorderJson, Options options)
  +static <T> void verifyJson(String json, boolean reorderJson, Function1Generic gsonBuilder)
  +static <T> void verifyJson(String json, boolean reorderJson, Function1Generic gsonBuilder, Options options)
  +static void verifyAsJson(Object o)
  +static void verifyAsJson(Object o, Options options)
  +static <T> void verifyAsJson(Object o, Function1Generic gsonBuilder)
  +static <T> void verifyAsJson(Object o, Function1Generic gsonBuilder, Options options)
}

class JsonUtils {
  +static String reorderFields(String json, Function1Generic gsonBuilder)
  +static String prettyPrint(String json, Function1Generic gsonBuilder)
  +static String asJson(Object o, Function1Generic gsonBuilder)
}

class Approvals {
  +static void verify(String formattedJson, Options options)
}

class Options {
  +Options forFile()
  +Options withExtension(String extension)
}

class Function1Generic {
  +TOut apply(TIn input)
}

JsonApprovals ..> JsonUtils
JsonApprovals ..> Approvals
JsonApprovals ..> Options
JsonApprovals ..> Function1Generic
JsonUtils ..> Function1Generic
Loading

File-Level Changes

Change Details Files
Make JsonApprovals’ Gson customizer parameters generic to avoid compile-time dependency on GsonBuilder, using type parameter T and raw Function1 casting at call sites.
  • Change verifyJson overloads to accept Function1<T,T> instead of Function1<GsonBuilder,GsonBuilder> and introduce a generic type parameter on the methods.
  • Add @SuppressWarnings("unchecked") on the overloads that forward the Function1 to JsonUtils, and cast the generic Function1 to a raw Function1 when calling JsonUtils.reorderFields/prettyPrint.
  • Similarly, change verifyAsJson overloads to use a generic type parameter T and cast the Function1 to raw when delegating to JsonUtils.asJson.
approvaltests/src/main/java/org/approvaltests/JsonApprovals.java
Update tests and documentation snapshot to align with the new generic signatures and keep existing Gson-based usages compiling via explicit type annotations.
  • Adjust approved markdown listing of verify* methods to reflect new line numbers/signatures in JsonApprovals after the generics changes.
  • Update JsonApprovalsTest usages of verifyAsJson with Gson customizers to include explicit GsonBuilder parameter types in lambdas so they match the new generic Function1<T,T> signature.
  • Update JsonFormattingTest to use a lambda with an explicit GsonBuilder parameter type when calling JsonApprovals.verifyJson with a Gson customizer.
approvaltests-tests/src/test/java/org/approvaltests/DocumentHelpersTest.listAllVerifyFunctions.approved.md
approvaltests-tests/src/test/java/org/approvaltests/JsonApprovalsTest.java
approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.java

Assessment against linked issues

Issue Objective Addressed Explanation
#768 Remove hard compile-time dependency on com.google.gson (GsonBuilder) from the main library API so that gson can remain an optional/transitive dependency without causing compilation or loading failures.
#768 Update tests and generated documentation to be consistent with the new JsonApprovals method signatures that no longer reference GsonBuilder directly.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing gson transitive dependency

1 participant