OpenAPITools / OpenAPITools/openapi-generator

[REQ] Improve php-laravel generator output when using _oneOf_

Open
#21,095 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

The generator output for a oneOf could be improved. I need properly typed models for each oneOf case. However, right now it seems to generate an object containing all possible properties.

Given this (minimal) example:

openapi: 3.0.0
info:
  title: Test oneOf
  version: 1
paths:
  /form-elements:
    get:
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  oneOf:
                    - $ref: '#/components/schemas/TextElement'
                    - $ref: '#/components/schemas/RadioElement'
components:
  schemas:
    TextElement:
      properties:
        type:
          type: string
          enum: [text]
        placeholder:
          type: string
    RadioElement:
      properties:
        type:
          type: string
          enum: [radio]
        options:
          type: array
          items:
            type: string

The php-laravel generator generates something like:

class FormElementsGet200ResponseInner {
    public function __construct(
        public \OpenAPI\Server\Model\RadioElementType $type,
        public string $placeholder,
        public array $options,
    ) {}

Where I would expect something more along the lines of e.g. the typescript-fetch generator:

export type FormElementsGet200ResponseInner = RadioElement | TextElement;
export function FormElementsGet200ResponseInnerFromJSON(json: any): FormElementsGet200ResponseInner {
    return FormElementsGet200ResponseInnerFromJSONTyped(json, false);
}
// .. other stuff

In addition I believe the \OpenAPI\Server\Model\RadioElementType in the constructor to be incorrect, as its type could also be TextElementType.

Describe the solution you'd like

I would like to extend support for at least oneOf in the php-laravel generator, so that the oneOf generates a proper set of models.

The generated code should be more usable that way. I would assume that fixing anyOf and allOf would be trivial once oneOf is working correctly.

Describe alternatives you've considered

I considered using the type as-is and creating the discriminator logic in my specific application, but I think it would be better to adapt the generator to be more precise.

Additional context

Requesting feedback here to check if the solution follows the general idea, before working on a PR. Would appreciate feedback

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal OpenAPI example in the issue and trace how the php-laravel generator handles its oneOf schema. The issue names no source files or tests, so locating the relevant generator model-processing entry point is part of the work. Done means oneOf produces usable alternative models rather than a combined model, with correct types for each case.

Written by the indexing model from the issue text.

Assessment

Tech stack
laravel, openapi, php
Domain
api, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.