Skip to content

Enhancement: allow expected in json_match (and json_schema) to be a plain string, not just a ContentReference object #30

@snytkine

Description

@snytkine

Summary

Currently the expected field of a json_match assertion must be a ContentReference object with type and content properties:

assertions:
  - type: json_match
    path: response.body.json
    expected:
      type: inline
      content: '{"id": 1, "name": "Alice"}'

The request body already supports a shorthand where the value can be a plain string (treated as inline content):

body: '{"id": 1}'          # plain string — works today for request body

The same convenience should be available for json_match (and json_schema) expected:

assertions:
  - type: json_match
    path: response.body.json
    expected: '{"id": 1, "name": "Alice"}'   # plain string — desired shorthand

When expected is a plain string it should be treated as type: inline with the string as content and an empty ignore list.


Note on Schema File Location

The authoritative YAML schema for test suites is src/main/resources/schemas/test-suite-schema.json — this is the file that is packaged with the application and used at runtime for validation. The file at .vscode/test-suite-schema.json is only a VS Code IDE helper copy and should not be edited directly.


Changes Required

1. JSON Schema (src/main/resources/schemas/test-suite-schema.json)

Change the expected definition for json_match from a bare $ref:

"expected": {
  "$ref": "#/$defs/ContentReference"
}

to a oneOf that accepts either a plain string or the full ContentReference object:

"expected": {
  "oneOf": [
    {
      "type": "string",
      "description": "Inline expected JSON (shorthand — equivalent to type: inline)"
    },
    {
      "$ref": "#/$defs/ContentReference"
    }
  ]
}

Apply the same change to json_schema's expected field for consistency.

2. YAML Deserialization

ObjectExpectedValue is currently a plain Java record (String type, String content, List<String> ignore). Jackson deserializes it only from an object node.

Add a custom Jackson deserializer (or use @JsonCreator / JsonDeserializer) so that:

  • A JSON string node → new ObjectExpectedValue("inline", theString, null)
  • A JSON object node → existing field-by-field mapping (unchanged)

3. No evaluator changes needed

JsonMatchAssertionEvaluator and JsonSchemaAssertionEvaluator already handle type == "inline" correctly, so no changes are needed there once deserialization normalises the plain-string form into an ObjectExpectedValue.


Files to Change

File Change
src/main/resources/schemas/test-suite-schema.json oneOf [string, ContentReference] for json_match and json_schema expected
src/main/java/.../model/ObjectExpectedValue.java Add custom Jackson deserializer to handle plain-string shorthand
src/test/java/.../model/ObjectExpectedValueDeserializerTest.java (new) Unit tests: plain string → ObjectExpectedValue("inline", …, null); object → normal mapping
src/test/java/.../service/TestSuiteLoaderTest.java Integration test: suite YAML using plain-string expected loads correctly

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions