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.77k stars 6.57k forks source link

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

Open creckord opened 1 year ago

creckord commented 1 year ago

Bug Report Checklist

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
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).

jimbydamonk commented 1 year ago

I too have hit this issue. Same problem. It seems that in the generate code, the URI datatype is using java.net.URI and not the one defined in my model.

creckord commented 1 year ago

@jimbydamonk that sounds like a slightly different problem. I specifically want to use java.net.URI here.

My issue is that the class using the URI property wrongly assumes it is a generated class with the expected validation methods the generator adds.

Ragin-LundF commented 1 year ago

We have a similar issue with those validateJsonObject methods. In our case, it is a oneOf declaration of multiple objects, and it generates in the end a TypeAdapter<Object> plus the real required TypeAdapters.

In theory, the oneOf (and maybe the allOf and anyOf) Sets of objects should not contain classes which the generator has not generated from the API to avoid calling this validateJsonObject method.

austpaul commented 1 year ago

Same issue for attributes representing binaries.

type: string
format: binary

Excerpt from compilation error:

 error: cannot find symbol
[ERROR]   symbol:   method validateJsonObject(JsonObject)
[ERROR]   location: class File
valepakh commented 1 year ago

Same thing with enums.

userkdg commented 1 year ago

up!

java.lang.IllegalArgumentException: The field `operator` in the JSON string is not defined in the `xxx` properties. JSON: {"id":158}
tai-tran-tan commented 1 year ago

I had the same problem generating by gradle plugin 6.6.0. Overcame by adding supportingFilesConstrainedTo = ["JSON"], don't know why it works. Doc openapi-generator-gradle-plugin Hope I can save a bit of your time.