Openapi-generator: [BUG] ClassCastException for any client

Created on 26 Aug 2019  路  7Comments  路  Source: OpenAPITools/openapi-generator

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) -> this website does not work.
  • [x] What's the version of OpenAPI Generator used? 4.1.1
  • [x] Have you search for related issues/PRs?
  • [x] What's the actual output vs expected output? See below
  • [ ] [Optional] Bounty to sponsor the fix (example)
Description

I tried to generate a java client for the openapi spec:

https://docs.graphhopper.com/openapi.yaml

But also other clients fail with the same exception. So maybe there is a problem with the spec but it should not fail with a ClassCastException IMO.

But I was able to create a Java client via pasting the spec into https://editor.swagger.io and so I assume the spec is roughly ok.

OpenAPI declaration file content or url

https://docs.graphhopper.com/openapi.yaml

Command line used for generation
java -jar openapi-generator-cli-4.1.1.jar generate -i https://docs.graphhopper.com/openapi.yaml -g java -o java
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: java (client)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'java' is considered stable.
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Environment variable JAVA_POST_PROCESS_FILE not defined so the Java code may not be properly formatted. To define it, try 'export JAVA_POST_PROCESS_FILE="/usr/local/bin/clang-format -i"' (Linux/Mac)
[main] INFO  o.o.c.languages.AbstractJavaCodegen - NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation getRoute
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation null
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation getIsochrone
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation getMatrix
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation postMatrix
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation calculateMatrix
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation getMatrixSolution
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation postGPX
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation getGeocode
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation solveVRP
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation getSolution
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: VehicleProfileId. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: VehicleProfileId. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: VehicleProfileId. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: VehicleProfileId. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: VehicleProfileId. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: VehicleProfileId. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: VehicleProfileId. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: VehicleProfileId. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - Unknown `format` int64 detected for type `number`. Defaulting to `number`
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: object. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: object. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: object. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: object. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: object. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: object. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: object. To fully utilize allOf, please use $ref instead of inline schema definition
[main] WARN  o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: object. To fully utilize allOf, please use $ref instead of inline schema definition
Exception in thread "main" java.lang.RuntimeException: Could not process model 'Request'.Please make sure that your schema is correct!
    at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:470)
    at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:920)
    at org.openapitools.codegen.cmd.Generate.run(Generate.java:400)
    at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:60)
Caused by: java.lang.ClassCastException: io.swagger.v3.oas.models.media.ObjectSchema cannot be cast to io.swagger.v3.oas.models.media.ArraySchema
    at org.openapitools.codegen.languages.AbstractJavaCodegen.toDefaultValue(AbstractJavaCodegen.java:721)
    at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:2058)
    at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:2271)
    at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:3706)
    at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:3654)
    at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:1918)
    at org.openapitools.codegen.languages.AbstractJavaCodegen.fromModel(AbstractJavaCodegen.java:942)
    at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1198)
    at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:465)
    ... 3 more
Java Bug Java

All 7 comments

馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

The problem is here:
https://github.com/OpenAPITools/openapi-generator/blob/d0d545bbdd409a177befa12cff809607751941f2/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java#L720-L721

For your Vehicle Schema, you have set minItems: 1. I am not sure if this make sense from an OpenAPI point of view. If you remove it, you will be able to use OpenAPI-Generator.


Anyway our ModelUtils returns true because of line 366:

https://github.com/OpenAPITools/openapi-generator/blob/d0d545bbdd409a177befa12cff809607751941f2/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java#L361-L370

We should not cast to ArraySchema without checking before.

The ClassCastException is fixed by https://github.com/OpenAPITools/openapi-generator/pull/3765

But because of your minItems: 1 under Vehicle our Generator will still think that #/components/schemas/Vehicle is an array.

I wait for your feedback on this.

Thanks a lot for this fast review and even a fix already! Do you create a jar via a CI pipeline somehow so that I can easily try the master once merged or this PR?

The minItems seem to be at the wrong place - thanks for the hint.

Do you create a jar via a CI pipeline?

Yes we are publishing master branch to https://oss.sonatype.org/content/repositories/snapshots/ which is more or less the maven-central location for SNAPSHOT versions.

The minItems seem to be at the wrong place - thanks for the hint.

If you remove it, then my fix is not necessary.

Ah, cool. This makes it even easier - thanks! It works now (a few warnings - will verify later if something is broken)

PR (fix) had been merged into master. Please pull the latest or use the SNAPSHOT to give it another try.

Thanks @jmini for the fix.

Was this page helpful?
0 / 5 - 0 ratings