OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.81k stars 6.58k forks source link

[BUG][JAVA] AnyOf enum or format uuid yields invalid code #16868

Open bewee-i opened 1 year ago

bewee-i commented 1 year ago

Bug Report Checklist

Description

Using anyOf in combination with enum and format: uuid yields invalid code.

openapi-generator version

7.0.1

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  version: 0.1.0
  title: ''
  description: ''
paths:
  /foo/:
    post:
      operationId: foo
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Patch_Uuid'
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                type: string
          description: ''
components:
  schemas:
    Patch_Uuid:
      anyOf:
      - type: string
        format: uuid
      - type: string
        enum:
        - delete
Generation Details

The generated PatchUuid.java contains:

public static void validateJsonElement(JsonElement jsonElement) throws IOException {
    // ...
    try {
        UUID.validateJsonElement(jsonElement);
        return;
    } catch (Exception e) {
        // ...
    }
    // ...
}

Because there is no such method validateJsonElement for class UUID, compilation fails. It should rather be sth like UUID.fromString(jsonElement.getAsString());

Steps to reproduce

Use the openapi-generator-maven-plugin to generate client code for okhttp-gson

sfc-gh-tjones commented 6 months ago

Bump - did you find a workaround for this? Hitting this as well on 7.4.0