{"id":"bd2c94a627a4669b","repo":"google/gson","slug":"invalid-version-version","errorCode":null,"errorMessage":"Invalid version: {version}","messagePattern":"Invalid version: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/GsonBuilder.java","lineNumber":207,"sourceCode":"  /**\n   * Configures Gson to enable versioning support. Versioning support works based on the annotation\n   * types {@link Since} and {@link Until}. It allows including or excluding fields and classes\n   * based on the specified version. See the documentation of these annotation types for more\n   * information.\n   *\n   * <p>By default versioning support is disabled and usage of {@code @Since} and {@code @Until} has\n   * no effect.\n   *\n   * @param version the version number to use.\n   * @return a reference to this {@code GsonBuilder} object to fulfill the \"Builder\" pattern\n   * @throws IllegalArgumentException if the version number is NaN or negative\n   * @see Since\n   * @see Until\n   */\n  @CanIgnoreReturnValue\n  public GsonBuilder setVersion(double version) {\n    if (Double.isNaN(version) || version < 0.0) {\n      throw new IllegalArgumentException(\"Invalid version: \" + version);\n    }\n    excluder = excluder.withVersion(version);\n    return this;\n  }\n\n  /**\n   * Configures Gson to excludes all class fields that have the specified modifiers. By default,\n   * Gson will exclude all fields marked {@code transient} or {@code static}. This method will\n   * override that behavior.\n   *\n   * <p>This is a convenience method which behaves as if an {@link ExclusionStrategy} which excludes\n   * these fields was {@linkplain #setExclusionStrategies(ExclusionStrategy...) registered with this\n   * builder}.\n   *\n   * @param modifiers the field modifiers. You must use the modifiers specified in the {@link\n   *     java.lang.reflect.Modifier} class. For example, {@link\n   *     java.lang.reflect.Modifier#TRANSIENT}, {@link java.lang.reflect.Modifier#STATIC}.\n   * @return a reference to this {@code GsonBuilder} object to fulfill the \"Builder\" pattern","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/GsonBuilder.java#L189-L225","documentation":"Thrown by GsonBuilder.setVersion(double) when the supplied version is NaN or negative. Versions drive the @Since and @Until field/class exclusion logic; NaN and negative numbers are meaningless as version ordinals and would break comparison. It is an IllegalArgumentException raised at configuration time, before any Gson instance is built.","triggerScenarios":"Calling setVersion(Double.NaN); setVersion(-1.0); computing the version from external config that yields NaN (e.g. parsing failure returning NaN) or a negative sentinel; passing 0.0 is allowed, but any negative value throws.","commonSituations":"Config-driven version selection where the config is missing and defaults to NaN/-1; arithmetic that divides by zero producing NaN; sentinel values like -1 meaning 'unset' fed directly to setVersion; unit tests with placeholder versions.","solutions":["Pass a valid non-negative finite double: setVersion(1.0), setVersion(2.5), or setVersion(0.0) to include everything.","If version comes from external config, validate it (Double.isFinite(v) && v >= 0.0) before calling setVersion, and default to 0.0 or skip setVersion entirely when unset.","Guard against NaN explicitly: if (Double.isNaN(v)) throw new IllegalArgumentException(\"version required\").","Use a sentinel-to-meaningful mapping (e.g. -1 -> 0.0) rather than passing the sentinel through."],"exampleFix":"// before: external config can yield NaN or negative\ndouble v = parseVersion(config); // may be NaN\nnew GsonBuilder().setVersion(v).create(); // throws if NaN or < 0\n\n// after: validate and default\nGsonBuilder b = new GsonBuilder();\ndouble v = parseVersion(config);\nif (Double.isFinite(v) && v >= 0.0) {\n  b.setVersion(v);\n}\nGson gson = b.create();","handlingStrategy":"validation","validationCode":"// Validate version before calling setVersion\ndouble v = parseVersion(config);\nif (!Double.isFinite(v) || v < 0.0) {\n  throw new IllegalArgumentException(\"Version must be finite and >= 0: \" + v);\n}\nGsonBuilder b = new GsonBuilder();\nif (v > 0) b.setVersion(v);\nGson gson = b.create();","typeGuard":"static boolean isValidVersion(double v) {\n  return Double.isFinite(v) && v >= 0.0;\n}","tryCatchPattern":null,"preventionTips":["Validate Double.isFinite(v) && v >= 0 before calling setVersion.","Default to skipping setVersion (versioning disabled) when the config value is absent.","Map sentinel values like -1 to 'disabled' rather than passing them through.","Guard against NaN from arithmetic (e.g. 0.0/0.0) at the source."],"tags":["gson-builder","versioning","configuration","validation","since-until"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}