Skip to content

Add OpenApiMockResponseHandler — spec-driven mock API with realistic test data#155

Open
Copilot wants to merge 8 commits intomasterfrom
copilot/add-openapi-mock-response-handler
Open

Add OpenApiMockResponseHandler — spec-driven mock API with realistic test data#155
Copilot wants to merge 8 commits intomasterfrom
copilot/add-openapi-mock-response-handler

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 2, 2026

  • Add net.datafaker:datafaker:1.9.0 dependency to openig-core/pom.xml
  • Refactor MockDataGenerator to use Datafaker for field-name-based data generation
  • Update MockDataGeneratorTest assertions to match Datafaker-backed (dynamic) values
  • Create petstore-mock.json test resource in openig-war/src/test/resources/routes/
  • Create IT_MockRoute.java integration test in openig-war/src/test/java/org/openidentityplatrform/openig/test/integration/
  • All 100 openig-core unit tests pass
Original prompt

Summary

Add an OpenApiMockResponseHandler that generates valid mock HTTP responses with realistic test data based on OpenAPI/Swagger specifications. This extends the existing OpenAPI validation infrastructure added in commit 80e6892.

Background

The repository already has:

  • OpenApiValidationFilter — validates requests/responses against OpenAPI specs
  • OpenApiRouteBuilder — auto-generates routes from OpenAPI spec files
  • OpenApiSpecLoader — parses OpenAPI/Swagger files via Swagger Parser

What's missing is a mock response generator that, instead of proxying to an upstream service (via ClientHandler), generates a valid JSON response based on the OpenAPI schema with realistic test data derived from field names, types, formats, enums, and examples.

Requirements

1. Create OpenApiMockResponseHandler (implements Handler)

Location: openig-core/src/main/java/org/forgerock/openig/handler/OpenApiMockResponseHandler.java

This handler should:

  • Parse the OpenAPI spec (reuse Swagger Parser already in dependencies)
  • Match incoming request path+method against spec paths (support path parameters like {petId})
  • Find the best response schema (prefer 200, then 201, then first 2xx, then default)
  • Recursively generate JSON from the schema:
    • Use example values from the spec if present (highest priority)
    • Use first enum value if defined
    • Handle $ref resolution
    • Handle composed schemas (allOf, oneOf, anyOf)
    • Handle object, array, and primitive types
    • Generate realistic data based on field names (see MockDataGenerator below)
  • Return proper HTTP response with Content-Type: application/json

Heaplet configuration:

{
  "name": "MockHandler",
  "type": "OpenApiMockResponseHandler",
  "config": {
    "spec": "${read('/path/to/openapi.yaml')}",
    "defaultStatusCode": 200,
    "arraySize": 3
  }
}

2. Create MockDataGenerator

Location: openig-core/src/main/java/org/forgerock/openig/handler/MockDataGenerator.java

A utility class that generates realistic values based on:

Priority order:

  1. Format-based: date, date-time, email, uri, uuid, ipv4, hostname, byte, password, etc.
  2. Field-name-based: Match field names (case-insensitive, ignore separators) against a built-in dictionary:
    • Personal: firstName → "John", lastName → "Doe", username → "johndoe", email → "john.doe@example.com"
    • Contact: phone → "+1-555-123-4567"
    • Address: city → "Springfield", country → "United States", zip → "62701"
    • Internet: url → "https://www.example.com", avatar → image URL
    • Text: description → sample text, title → "Sample Title"
    • Business: company → "Acme Corporation", role → "admin"
    • Auth: token → mock JWT, password → "P@ssw0rd123!"
    • Numbers: id → 1001, age → 30, price → 29.99, quantity → 5
    • Booleans: enabled/active/verified → true, deleted/archived → false
  3. Type-based fallback: generic string/number/boolean

The generator should be deterministic (seeded Random) for reproducible tests.
It should respect minimum, maximum, minLength, maxLength constraints from the schema.

3. Register the alias in CoreClassAliasResolver

Add OpenApiMockResponseHandler alias in openig-core/src/main/java/org/forgerock/openig/alias/CoreClassAliasResolver.java:

ALIASES.put("OpenApiMockResponseHandler", OpenApiMockResponseHandler.class);

4. Integrate mock mode into OpenApiRouteBuilder

Modify OpenApiRouteBuilder.buildRouteJson() to accept a mockMode boolean parameter. When mockMode is true:

  • Add OpenApiMockResponseHandler to the heap instead of using ClientHandler
  • The chain becomes: OpenApiValidationFilterOpenApiMockResponseHandler

5. Integrate into RouterHandler

Add mockMode setting to OpenApiValidationSettings:

public static final class OpenApiValidationSettings {
    public final boolean enabled;
    public final boolean failOnResponseViolation;
    public final boolean mockMode;  // NEW
}

Update the Heaplet to read openApiValidation.mockMode from config:

{
  "handler": {
    "type": "Router",
    "config": {
      "directory": "/opt/openig/config/routes",
      "openApiValidation": {
        "enabled": true,
        "failOnResponseViolation": true,
        "mockMode": true
      }
    }
  }
}

6. Add unit tests

Create openig-core/src/test/java/org/forgerock/openig/handler/OpenApiMockResponseHandlerTest.java:

  • Test valid GET request returns 200 with generated JSON matching schema
  • Test path parameter matching works
  • Test enum values are used from schema
  • Test example values from schema take priority
  • Test array responses generate correct number of items
  • Test nested object schemas are handled recursively
  • Test allOf/oneOf/anyOf composed schemas
  • Test 404 for unmatched paths
  • Test 405 for wrong method
  • Test Heaplet...

This pull request was created from Copilot chat.

@vharseko vharseko assigned maximthomas and unassigned vharseko Apr 2, 2026
Copilot AI changed the title [WIP] Add OpenApiMockResponseHandler for generating mock HTTP responses Add OpenApiMockResponseHandler — spec-driven mock API with realistic test data Apr 2, 2026
Copilot AI requested a review from vharseko April 2, 2026 11:13
…nature (#157)

Co-authored-by: Valery Kharseko <vharseko@3a-systems.ru>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: vharseko <6818498+vharseko@users.noreply.github.com>
@vharseko vharseko requested a review from maximthomas April 3, 2026 10:25
@vharseko vharseko marked this pull request as ready for review April 3, 2026 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants