Skip to content

Conversation

@the-wunmi
Copy link

@the-wunmi the-wunmi commented Dec 5, 2025

Summary

This PR implements the missing decompressed parameter for kong.service.response.get_body() that was documented in the PDK reference but never actually implemented.

Problem:
The kong.service.response.get_body() function has been documented with an optional decompressed parameter, but the parameter was never implemented. This causes the function to return nil when trying to parse compressed response bodies (e.g., gzip-compressed JSON) because it attempts to parse the compressed data directly, which fails.

Example of current broken behavior:

  • Service returns gzip-compressed JSON with Content-Type: application/json and Content-Encoding: gzip
  • Calling kong.service.response.get_body() attempts to JSON-decode the gzipped bytes
  • Returns nil with error "invalid json body"

Solution:

  • Added support for the decompressed parameter in kong/pdk/service/response.lua
  • When decompressed=true, the function now:
    1. Checks the Content-Encoding header
    2. If the encoding is gzip, decompresses the body using kong.tools.gzip.inflate_gzip()
    3. Returns the decompressed body for content-type specific parsing
    4. Returns appropriate error messages if decompression fails

Implementation Details:

  • Leverages existing kong.tools.gzip module for gzip decompression
  • Maintains backward compatibility - when decompressed is not provided or false, behavior is unchanged (still returns nil for compressed bodies)
  • Handles decompression errors gracefully with descriptive error messages

Testing Clarification Needed

The contributing guidelines recommend Busted tests, but t/01-pdk/07-service-response/05-get_body.t exists with only a TODO placeholder. Additionally, there's no clear pattern for testing kong.service.response.* functions that require actual upstream responses.

Need guidance on:

  • Should tests be added to t/01-pdk/07-service-response/05-get_body.t (Nginx test), spec/ (Busted), or both?
  • What's the preferred approach for testing service response functions like this?

Once I understand the testing approach, I'll add tests covering:

  • Decompressed gzip bodies with various content types (JSON, form-data, etc.)
  • Default behavior (no decompression)
  • Error handling for invalid gzip data

Checklist

@CLAassistant
Copy link

CLAassistant commented Dec 5, 2025

CLA assistant check
All committers have signed the CLA.

@the-wunmi the-wunmi marked this pull request as ready for review December 5, 2025 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants