Skip to content

[BUG][JAVA] Invalid validateJsonObject code with okhttp-gson #14041

@creckord

Description

@creckord

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
    see below
Description

We frequently specialize base types in order to indicate specific domain meaning and to apply domain constraints centrally.
E.g.:

    Uri:
      type: string
      format: uri

We also frequently "abuse" allOf to combine property-specific documentation with schema references:

    Callback:
      properties:
        href:
          description: property description here
          allOf:
            - $ref: '#/components/schemas/Uri'

When these two (string subtype plus allOf) come together, it breaks the generated validateJsonObject:

  public static void validateJsonObject(JsonObject jsonObj) throws IOException {
      //...
      URI.validateJsonObject(jsonObj.getAsJsonObject("href"));
      //    ^^^ cannot find symbol
  }

oneOf is similarly broken.

openapi-generator version

6.2.1

Regression: I upgraded from 5.4.0, where it still worked, to 6.2.1.

The error goes back to 6.0.0 with the new okhttp-gson-nextgen templates

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: validateJsonObject
  version: "0"
  description: Minimal example for invalid validateJsonObject
paths:
  /:
    get:
      responses:
        '200':
          description: example path
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Callback'
components:
  schemas:
    Uri:
      type: string
      format: uri
    Callback:
      properties:
        href:
          description: this allOf triggers the bug
          allOf:
            - $ref: '#/components/schemas/Uri'
Generation Details
openapi-generator generate \
    -i /project/src/main/resources/openapi.yaml \
    -o /tmp/openapi \
    -g java \
    --api-package org.example.api \
    --model-package org.example.model \
    -p java8=true,dateLibrary=java8,hideGenerationTimestamp=true,sourceFolder=java
Steps to reproduce
  • Generate code for the given schema using the above generator options
  • Try to compile the code / have a look at generated Callback.java class
Suggest a fix

The generator needs to check if the target type of a field to be validated is actually under its control, or skip the nested validate code.

Also, in our case, the additional validation isn't needed at all (validation happens on the returned values anyway). It would be awesome to have a generator option to skip the whole thing, including the CustomTypeAdapterFactory with its double conversion (JsonObject, bean).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions