Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8217627

HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior

    Details

    • Subcomponent:
    • Resolved In Build:
      b06
    • Verification:
      Not verified

      Description

      The API documentation of BodySubscribers::mapping says:

      ```
               * <p> The mapping function is executed using the client's {@linkplain
               * HttpClient#executor() executor}, and can therefore be used to map any
               * response body type, including blocking {@link InputStream}, as shown
               * in the following example which uses a well-known JSON parser to
               * convert an {@code InputStream} into any annotated Java type.
               *
               * <p>For example:
               * <pre> {@code public static <W> BodySubscriber<W> asJSON(Class<W> targetType) {
               * BodySubscriber<InputStream> upstream = BodySubscribers.ofInputStream();
               *
               * BodySubscriber<W> downstream = BodySubscribers.mapping(
               * upstream,
               * (InputStream is) -> {
               * try (InputStream stream = is) {
               * ObjectMapper objectMapper = new ObjectMapper();
               * return objectMapper.readValue(stream, targetType);
               * } catch (IOException e) {
               * throw new UncheckedIOException(e);
               * }
               * });
               * return downstream;
               * } }</pre>
      ```
      Blocking an executor thread can lead to deadlocks, or starve the HttpClient's executor of threads, and therefore should not be promoted by the API documentation.
      Instead we should promote e.g. returning a Supplier<W> in the case where creating an instance of W would block.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                dfuchs Daniel Fuchs
                Reporter:
                dfuchs Daniel Fuchs
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: