specmatic icon indicating copy to clipboard operation
specmatic copied to clipboard

Enhance parsedValue function with optional contentType parameter for improved parsing accuracy

Open Copilot opened this issue 8 months ago • 1 comments

Problem

The current parsedValue function in Grammar.kt uses content-based guessing to determine how to parse input strings (JSON, XML, or plain text). This approach can be unreliable and doesn't leverage available content type information from HTTP contexts.

Solution

Enhanced the parsedValue function to accept an optional contentType parameter that provides explicit parsing hints:

Core Changes

  1. Enhanced Function Signature

    // Before
    fun parsedValue(content: String?): Value
    
    // After  
    fun parsedValue(content: String?, contentType: String? = null): Value
    
  2. Strict Content Type Parsing When contentType is provided, the function parses strictly according to the hint:

    • application/json → Parse as JSON (objects/arrays only, throws exception for invalid JSON)
    • application/xml, text/xml → Parse as XML
    • text/*, text/plain → Parse as string (no guessing)
    • Unknown types → Fall back to existing guessing behavior
  3. Backward Compatibility When contentType is null, maintains the existing guessing behavior for full compatibility.

Updated Usage Points

Updated key classes to pass content type hints where available:

  • HttpRequest.updateBody() - Uses Content-Type header from request
  • HttpResponse constructor - Uses Content-Type header from response headers
  • ExampleRequestBuilder - Uses known content type when building examples

Example Usage

// Strict JSON parsing with content type hint
val jsonValue = parsedValue("""{"key": "value"}""", "application/json")

// Strict text parsing (won't try to parse as JSON)
val textValue = parsedValue("""{"not": "json"}""", "text/plain") 

// Fallback to existing guessing behavior
val guessedValue = parsedValue("""{"might": "be json"}""", null)

Testing

Added comprehensive test coverage including:

  • Strict parsing for each supported content type
  • Error handling for invalid content with strict types
  • Charset handling (application/json; charset=utf-8)
  • Case insensitive content type matching
  • Fallback behavior for unknown content types
  • Backward compatibility with null content type

Benefits

  • Improved Accuracy: Leverages explicit content type information when available
  • Better Error Handling: Strict parsing provides clearer error messages for malformed content
  • Backward Compatible: No breaking changes to existing code
  • Surgical Implementation: Minimal changes focused only on the core functionality

All existing tests pass, ensuring no regressions in current functionality.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Jun 23 '25 13:06 Copilot

Handle readValue in Postman.kt, HttpHeadersPattern.kt.

Updated parsedValue calls in Postman.kt to use content type hints where available:

  • Response parsing now uses Content-Type header from response headers
  • Request parsing uses Content-Type header for raw body content

For HttpHeadersPattern.kt, the parsedValue calls are for parsing content-type header values themselves (not content guided by content type), so no changes were needed there.

See commit f8c5d61 for the implementation.

Copilot avatar Jun 23 '25 14:06 Copilot