specmatic icon indicating copy to clipboard operation
specmatic copied to clipboard

Implement HasDefaultExample support in JSONObjectPattern

Open Copilot opened this issue 8 months ago • 2 comments

Overview

This PR implements support for the HasDefaultExample interface in JSONObjectPattern, bringing consistency with other pattern types like NumberPattern, StringPattern, and BooleanPattern.

Problem

JSONObjectPattern was the only major pattern type that didn't implement HasDefaultExample, creating inconsistency in the pattern system and preventing JSON object patterns from supporting default examples.

Solution

Changes Made

  1. Enhanced JSONObjectPattern class:

    • Added HasDefaultExample interface to the class declaration
    • Added override val example: Any? = null parameter to constructor
    • Maintains full backward compatibility with existing code
  2. Comprehensive test coverage:

    • Added 9 new focused tests in HasDefaultExampleTests nested class
    • Tests cover interface implementation, example retrieval, and utility function integration
    • Verifies backward compatibility with existing constructor patterns

Code Example

// Before - JSONObjectPattern didn't support examples
val pattern1 = JSONObjectPattern(mapOf("id" to NumberPattern()))
// pattern1 is not HasDefaultExample

// After - JSONObjectPattern now supports examples  
val pattern2 = JSONObjectPattern(
    pattern = mapOf("id" to NumberPattern()),
    example = """{"id": 123, "name": "John"}"""
)
// pattern2 implements HasDefaultExample
println(hasExample(pattern2)) // true

Consistency with Other Patterns

This implementation follows the same pattern as other classes:

  • NumberPattern: override val example: String? = null
  • StringPattern: override val example: String? = null
  • JSONObjectPattern: override val example: Any? = null (new)

The Any? type allows JSONObjectPattern to support both JSON string representations and map objects as examples.

Testing

  • ✅ All existing JSONObjectPattern tests pass (140+ tests)
  • ✅ All SchemaExampleUtils tests pass
  • ✅ New HasDefaultExample functionality fully tested
  • ✅ Backward compatibility verified
  • ✅ Integration with utility functions confirmed

Benefits

  • Consistency: All major pattern types now implement HasDefaultExample
  • Functionality: JSON object patterns can now provide default examples
  • Integration: Works seamlessly with existing example utility functions
  • Backward Compatible: No breaking changes to existing code

[!WARNING]

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/aquasecurity/trivy
    • Triggering command: REDACTED, pid is -1 (http block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 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 28 '25 12:06 Copilot

  • Add a test where there's a super-simple OpenAPI spec with an object schema in the request (all keys mandatory), having an example in the object schema. Run tests from it, and ensure that the example...

Added comprehensive integration tests as requested:

Request scenarios:

  • Simple JSON object examples in request bodies with contract testing verification
  • Complex nested object examples in request bodies with contract testing verification

Response scenarios:

  • JSON object examples in response bodies with stub testing verification
  • JSON object examples in array responses with stub testing verification

The tests demonstrate end-to-end functionality with real OpenAPI specs, covering both simple and complex object schemas with examples. All scenarios verify that examples are properly used in both test requests and stub responses. Commit: 66c774d

Copilot avatar Jun 28 '25 13:06 Copilot

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

[!WARNING]

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/aquasecurity/trivy
    • Triggering command: REDACTED, pid is -1 (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot avatar Jun 28 '25 14:06 Copilot