{"record":{"id":"39bb78c5e8b76404","repo":"google/gson","slug":"failed-parsing-s-as-inetaddress-at-path-pa","errorCode":null,"errorMessage":"Failed parsing '${s}' as InetAddress; at path ${path}; to allow DNS addresses, set system property gson.allowDnsInetAddress to \"true\"","messagePattern":"Failed parsing '(.+?)' as InetAddress; at path (.+?); to allow DNS addresses, set system property gson\\.allowDnsInetAddress to \"true\"","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":760,"sourceCode":"  public static final TypeAdapter<InetAddress> INET_ADDRESS =\n      new TypeAdapter<InetAddress>() {\n\n        // A pattern that matches every IP address and no DNS address. It matches plenty of things\n        // that aren't either of those, which is fine. An IPv4 address is n.n.n.n where each n is a\n        // non-negative integer. An IPv6 address contains at least one colon. (There are further\n        // constraints in both cases, but they don't matter here.)\n        private final Pattern ipAddressPattern = Pattern.compile(\".*:.*|[0-9]+(\\\\.[0-9]+){3}\");\n\n        @Override\n        public InetAddress read(JsonReader in) throws IOException {\n          if (in.peek() == JsonToken.NULL) {\n            in.nextNull();\n            return null;\n          }\n          String s = in.nextString();\n          if (!ipAddressPattern.matcher(s).matches()\n              && !Boolean.getBoolean(\"gson.allowDnsInetAddress\")) {\n            throw new JsonSyntaxException(\n                \"Failed parsing '\"\n                    + s\n                    + \"' as InetAddress; at path \"\n                    + in.getPreviousPath()\n                    + \"; to allow DNS addresses, set system property gson.allowDnsInetAddress to\"\n                    + \" \\\"true\\\"\");\n          }\n          @SuppressWarnings(\"AddressSelection\")\n          InetAddress addr = InetAddress.getByName(s);\n          return addr;\n        }\n\n        @Override\n        public void write(JsonWriter out, InetAddress value) throws IOException {\n          out.value(value == null ? null : value.getHostAddress());\n        }\n      };\n","sourceCodeStart":742,"sourceCodeEnd":778,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L742-L778","documentation":"Thrown by Gson's InetAddress TypeAdapter when the JSON string does not look like an IP address. The adapter uses a regex (matches IPv4 dotted-quad or anything containing a colon for IPv6) and by default rejects DNS hostnames unless the system property gson.allowDnsInetAddress is set to \"true\", because resolving a hostname triggers a blocking DNS lookup.","triggerScenarios":"Deserializing a field of type java.net.InetAddress (or Inet4Address/Inet6Address subtypes) from a JSON value containing a hostname like \"example.com\" or \"my-host\", which fails the ipAddressPattern regex and the system property is not set.","commonSituations":"Config payloads that carry hostnames rather than IPs; service discovery records; ingesting machine inventories where the JSON has DNS names; default Gson config where DNS lookups during deserialization are intentionally disabled to avoid I/O on the parsing thread.","solutions":["If DNS resolution during deserialization is acceptable, set system property gson.allowDnsInetAddress=true (e.g. -Dgson.allowDnsInetAddress=true at JVM startup, or System.setProperty(...) before parsing).","Deserialize the field as String and resolve with InetAddress.getByName(...) explicitly in your own code where you can control timeouts and error handling.","Register a custom TypeAdapter<InetAddress> that resolves hostnames with a configured DNS resolver and timeout.","Fix the source data to contain IP addresses (e.g. 192.0.2.1 or 2001:db8::1)."],"exampleFix":"// before\npublic class Host { public InetAddress address; }\n// JSON: {\"address\":\"my-service.internal\"} -> fails\n\n// option 1: allow DNS at JVM level\njava -Dgson.allowDnsInetAddress=true -jar app.jar\n\n// option 2: deserialize as String, resolve explicitly\npublic class Host {\n  public String address;\n  public InetAddress resolved() throws UnknownHostException {\n    return InetAddress.getByName(address);\n  }\n}","handlingStrategy":"validation","validationCode":"private static final Pattern IP =\n  Pattern.compile(\"^[0-9]+(\\\\.[0-9]+){3}$|:.*\"); // mirrors gson's check\nString raw = jsonNode.get(\"address\").getAsString();\nboolean allowDns = Boolean.getBoolean(\"gson.allowDnsInetAddress\");\nif (!allowDns && !IP.matcher(raw).matches()) {\n  throw new IllegalArgumentException(\"Not an IP and DNS disabled: \" + raw);\n}","typeGuard":null,"tryCatchPattern":"try {\n  return gson.fromJson(json, Host.class);\n} catch (JsonSyntaxException e) {\n  if (e.getMessage().contains(\"as InetAddress\")) {\n    // either fix data to IP form, or set gson.allowDnsInetAddress=true and retry\n    throw new IllegalArgumentException(\"InetAddress needs IP or DNS allowed\", e);\n  }\n  throw e;\n}","preventionTips":["Deserialize hostnames as String and resolve explicitly with a timeout-aware resolver.","Set -Dgson.allowDnsInetAddress=true only in environments where blocking DNS in parse is acceptable.","Run unit tests asserting both IPv4 and IPv6 inputs parse cleanly.","Document which fields are IP-only vs DNS-capable to prevent confusion."],"tags":["gson","parsing","inet-address","dns","system-property","json-syntax-exception"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}