Add OpenApiMockResponseHandler — spec-driven mock API with realistic test data#155
Open
Add OpenApiMockResponseHandler — spec-driven mock API with realistic test data#155
Conversation
…pport Agent-Logs-Url: https://github.com/OpenIdentityPlatform/OpenIG/sessions/d5ffaefb-3274-41bc-a5c6-bd2f7131e0bb Co-authored-by: vharseko <6818498+vharseko@users.noreply.github.com>
Agent-Logs-Url: https://github.com/OpenIdentityPlatform/OpenIG/sessions/d5ffaefb-3274-41bc-a5c6-bd2f7131e0bb Co-authored-by: vharseko <6818498+vharseko@users.noreply.github.com>
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
… integration test Agent-Logs-Url: https://github.com/OpenIdentityPlatform/OpenIG/sessions/145b9565-daac-42f5-849e-1a38a73bf63f Co-authored-by: vharseko <6818498+vharseko@users.noreply.github.com>
maximthomas
reviewed
Apr 2, 2026
openig-core/src/main/java/org/forgerock/openig/handler/MockDataGenerator.java
Show resolved
Hide resolved
6 tasks
…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>
maximthomas
approved these changes
Apr 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
net.datafaker:datafaker:1.9.0dependency toopenig-core/pom.xmlMockDataGeneratorto use Datafaker for field-name-based data generationMockDataGeneratorTestassertions to match Datafaker-backed (dynamic) valuespetstore-mock.jsontest resource inopenig-war/src/test/resources/routes/IT_MockRoute.javaintegration test inopenig-war/src/test/java/org/openidentityplatrform/openig/test/integration/Original prompt
Summary
Add an
OpenApiMockResponseHandlerthat 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 specsOpenApiRouteBuilder— auto-generates routes from OpenAPI spec filesOpenApiSpecLoader— parses OpenAPI/Swagger files via Swagger ParserWhat'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(implementsHandler)Location:
openig-core/src/main/java/org/forgerock/openig/handler/OpenApiMockResponseHandler.javaThis handler should:
{petId})examplevalues from the spec if present (highest priority)enumvalue if defined$refresolutionallOf,oneOf,anyOf)object,array, and primitive typesContent-Type: application/jsonHeaplet configuration:
{ "name": "MockHandler", "type": "OpenApiMockResponseHandler", "config": { "spec": "${read('/path/to/openapi.yaml')}", "defaultStatusCode": 200, "arraySize": 3 } }2. Create
MockDataGeneratorLocation:
openig-core/src/main/java/org/forgerock/openig/handler/MockDataGenerator.javaA utility class that generates realistic values based on:
Priority order:
date,date-time,email,uri,uuid,ipv4,hostname,byte,password, etc.firstName→ "John",lastName→ "Doe",username→ "johndoe",email→ "john.doe@example.com"phone→ "+1-555-123-4567"city→ "Springfield",country→ "United States",zip→ "62701"url→ "https://www.example.com",avatar→ image URLdescription→ sample text,title→ "Sample Title"company→ "Acme Corporation",role→ "admin"token→ mock JWT,password→ "P@ssw0rd123!"id→ 1001,age→ 30,price→ 29.99,quantity→ 5enabled/active/verified→ true,deleted/archived→ falseThe generator should be deterministic (seeded Random) for reproducible tests.
It should respect
minimum,maximum,minLength,maxLengthconstraints from the schema.3. Register the alias in
CoreClassAliasResolverAdd
OpenApiMockResponseHandleralias inopenig-core/src/main/java/org/forgerock/openig/alias/CoreClassAliasResolver.java:4. Integrate mock mode into
OpenApiRouteBuilderModify
OpenApiRouteBuilder.buildRouteJson()to accept amockModeboolean parameter. WhenmockModeis true:OpenApiMockResponseHandlerto the heap instead of usingClientHandlerOpenApiValidationFilter→OpenApiMockResponseHandler5. Integrate into
RouterHandlerAdd
mockModesetting toOpenApiValidationSettings:Update the Heaplet to read
openApiValidation.mockModefrom 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:This pull request was created from Copilot chat.