Unable to send multiple media types in header

Issue #98 resolved
Former user created an issue

I'm sending multiple media types in header but swagger is unable to parse these values.

Content Type: application/json, application/xml

I have debugged the issue and found that RequsetValidator class is using MediaType.parse method for parsing of the header values and throwing exception.

@Nonnull
    private ValidationReport validateMediaTypes(@Nonnull final Request request,
                                                @Nonnull final String headerName,
                                                @Nonnull final Collection<String> specMediaTypes,
                                                @Nonnull final String invalidTypeKey,
                                                @Nonnull final String notAllowedKey) {

        final Collection<String> requestHeaderValues = request.getHeaderValues(headerName);
        if (requestHeaderValues.isEmpty()) {
            return empty();
        }

        final List<MediaType> requestMediaTypes = new ArrayList<>();
        for (final String requestHeaderValue : requestHeaderValues) {
            try {
                requestMediaTypes.add(MediaType.parse(requestHeaderValue));
            } catch (final IllegalArgumentException e) {
                return ValidationReport.singleton(messages.get(invalidTypeKey, requestHeaderValue));
            }
        }

Comments (5)

  1. James Navin

    This looks very similar to Issue #97.

    The Request implementation is supposed to split out the headers into a collection before it reaches this point. Can I ask what adapter you used, or how you constructed the Request object? I think the bug is there rather than in the validate code.

    Thanks.

  2. James Navin

    Thanks for the clarification. I think this one will be fixed with the fix for issue #97. I'll update this ticket when its available.

  3. James Navin

    @ooxaam - the proposed fix for this is available in v1.3.9+

    I'll close this ticket on the assumption that you issue is resolved. If it isn't, feel free to re-open the ticket.

  4. Log in to comment