Skip to content

Feature Request: Auto-Generate Response Examples from OpenAPI Schemas #39

Description

@k2tzumi

Description:

This feature proposes extending the existing eg-r2 library functionality to automatically generate example responses based on OpenAPI schemas defined within PHP classes. Currently, eg-r2 automates route generation (Dynamic Route Generation from OpenAPI Operations), request validation (Schema-Driven Request Validation Rules), and FormRequest property population (Automated FormRequest Property Population). By adding response example generation, the library can further enhance the API development workflow, potentially serving as a mock server.

The core idea is to process OpenAPI annotations (such as OpenApi\Annotations\Schema and OpenApi\Attributes\Property) within response classes (e.g., classes used to define the structure of API responses) to produce valid example JSON/YAML payloads.

Proposed Functionality:

  1. Schema-Driven Example Data Generation:
  1. Mock Server Capabilities (Future Extension):
  • Endpoint Mapping: Map API endpoints (path, HTTP method) to their respective response schemas.
    Dynamic Responses: Potentially generate different examples based on path parameters, query parameters, or request body content.
  • HTTP Status Code Support: Generate examples for various HTTP status codes (e.g., 200 OK, 422 Unprocessable Entity, 401 Unauthorized) to provide more realistic mocks.
  1. Custom Generation Rules:
  • Extensible Generators: Provide hooks or extension points to allow users to define custom data generation logic for specific field names or data types (e.g., always generate a UUID for user_id).
  • Configuration Management: Manage custom generation rules via a configuration file (e.g., eg_r2.php, similar to stubs/config/eg_r2.php) for flexible customization.

Benefits:

  • Accelerated Frontend Development: Frontend teams can work against realistic API mocks without waiting for backend implementation.
  • Improved API Design: Forces a clear definition of API responses early in the development cycle.
  • Enhanced Documentation: Automatically generated examples can enrich API documentation.
  • Reduced Development Overhead: Automates a manual and error-prone task of creating example payloads.
    This feature would significantly enhance the utility of eg-r2 by extending its contract-first development capabilities from requests to responses, streamlining the overall API development process

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions