openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][JAVA] oneOf/anyOf validateJsonElement cannot find symbol

Open Bethibande opened this issue 1 year ago • 0 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The java generator using okhttp-gson generates code that doesn't compile because of .validateJsonElement calls on Objects that don't have this method. This happens with both oneOf and anyOf types.

// validate the json string with OffsetDateTime
try {
    OffsetDateTime.validateJsonElement(jsonElement);
    return;
} catch (Exception e) {
    errorMessages.add(String.format("Deserialization for OffsetDateTime failed with `%s`.", e.getMessage()));
    // continue to the next one
}

Gradle logs: https://gist.github.com/Bethibande/9582795e2e1fd47695019d8f1c685a8b

openapi-generator version

latest/master

OpenAPI declaration file content or url

https://gist.github.com/Bethibande/55942a3f4e671d7352257c3833b154d6

Suggest a fix

When validating json elements, the fallback for types that do not have any special handling like arrays or primitives is always to call the static .validateJsonElement of the type. That doesn't work for non-generated types like OffsetDateTime though.

{{^isPrimitiveType}}
     {{{dataType}}}.validateJsonElement(jsonElement);
    actualAdapter = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}};
{{/isPrimitiveType}}

It has to be checked whether or not we are dealing with a type that has a validateJsonElement method (a generated model class?), if not, other validation checks have to be performed.

Bethibande avatar May 01 '24 12:05 Bethibande