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

[BUG] Java Client com.google.gson.JsonSyntaxException when using oneOf

Open R4NN4 opened this issue 2 years ago • 4 comments

Description

openapi-generator-maven-plugin release version 5.1.0

A Java client, generated with openapi-generator-maven-plugin will throw a com.google.gson.JsonSyntaxException during JSON deserialization of a server response:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 44 path $[0].value
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
	at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
	at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
	at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
	at com.google.gson.Gson.fromJson(Gson.java:932)
	at com.google.gson.Gson.fromJson(Gson.java:897)
	at com.google.gson.Gson.fromJson(Gson.java:846)

Server response JSON:

[{"value":1000.0}]

Minimal example spec:

openapi: 3.0.0
info:
  description: Minimal Spec
  version: 1.0.0
  title: Test
servers:
- url: test
  description: Service name
paths:
  "/read":
    post:
      operationId: readList
      description: Read list of values
      responses:
        '200':
          "$ref": "#/components/responses/ValueList"
components:
  responses:
    ValueList:
      description: List of value list objects
      content:
        application/json:
          schema:
            "$ref": "#/components/schemas/ValueList"
  schemas:
    ValueList:
      description: Array of value objects
      type: array
      items:
        "$ref": "#/components/schemas/ValueObject"
    ValueObject:
      type: object
      description: A value object
      required:
      - value
      properties:
        value:
          "$ref": "#/components/schemas/Value"
    Value:
      description: Value datatype
      oneOf:
      - "$ref": "#/components/schemas/Scalar"
      - "$ref": "#/components/schemas/Array"
    Scalar:
      description: Scalar datatype
      oneOf:
      - type: string
        maxLength: 1089
      - type: number
      - type: boolean
    Array:
      type: array
      minItems: 1
      items:
        "$ref": "#/components/schemas/Scalar"

Because of the exception further processing is interrupted.

openapi-generator version

openapi-generator-maven-plugin release version 5.1.0

Generation Details

Following maven pom.xml is used to generate the Java Client:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

         <build>
               <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-clean-plugin</artifactId>
				<version>3.1.0</version>
				<configuration>
					<filesets>
						<fileset>
							<directory>src-gen</directory>
							<includes>
								<include>**/*</include>
							</includes>
						</fileset>
					</filesets>
				</configuration>
			      </plugin>

                              <plugin>
				<groupId>org.openapitools</groupId>
				<artifactId>openapi-generator-maven-plugin</artifactId>
				<!-- RELEASE_VERSION -->
				<version>5.1.0</version>
				<!-- /RELEASE_VERSION -->
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<inputSpec>http://.../api.yaml</inputSpec>
							<generatorName>java</generatorName>
							<output>${project.basedir}</output>
							<generateModelTests>false</generateModelTests>
							<generateApiTests>false</generateApiTests>
							<packageName>at.internal.test</packageName>
							<apiPackage>at.internal.test</apiPackage>
							<modelPackage>at.internal.test.model</modelPackage>
							<invokerPackage>at.internal.test.invoker</invokerPackage>
							<withXml>false</withXml>
							<configOptions>
								<sourceFolder>src-gen</sourceFolder>
							</configOptions>
							<verbose>true</verbose>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
Steps to reproduce
  • Generate Java client and server with example spec
  • Generate client request / server response
  • Server should response with JSON
  • Exception will be thrown on client
Related issues/PRs

--

Suggest a fix

--

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)?
  • [ ] Have you tested with the latest master to confirm the issue still exists? -> Not possible -> https://github.com/OpenAPITools/openapi-generator/issues/12556
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?

R4NN4 avatar Jun 17 '22 12:06 R4NN4

can you please try the latest stable version v6.0.0 ?

wing328 avatar Jun 17 '22 12:06 wing328

Tried with latest version v6.0.0, but the Problem was that the generated source could not be compiled. Used the same pom.xml as above but with:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
...
				<!-- RELEASE_VERSION -->
				<version>6.0.0</version>
				<!-- /RELEASE_VERSION -->
...
</project>

Got following compile errors:

[ERROR] Failed to execute goal org.eclipse.tycho:tycho-compiler-plugin:2.5.0:compile (default-compile) on project xx.xxxx.xxx.xxx.xxx: Compilation failure: Compilation failure: 
[ERROR] D:\ws9494\xxx_G\plugins\xx.xxxx.xxx.xxx.xxx\src-gen\at\xxx\xxx\xxx\xxx\xxx\xxx\Scalar.java:[65] 
[ERROR] 	final TypeAdapter<BigDecimal> adapterBigDecimal = gson.getDelegateAdapter(this, TypeToken.get(BigDecimal.class));
[ERROR] 	                  ^^^^^^^^^^
[ERROR] BigDecimal cannot be resolved to a type
[ERROR] D:\ws9494\xxx_G\plugins\xx.xxxx.xxx.xxx.xxx\src-gen\at\xxx\xxx\xxx\xxx\xxx\xxx\Scalar.java:[65] 
[ERROR] 	final TypeAdapter<BigDecimal> adapterBigDecimal = gson.getDelegateAdapter(this, TypeToken.get(BigDecimal.class));
...
[ERROR] 	                                                                                              ^^^^^^^^^^
[ERROR] BigDecimal cannot be resolved to a type
[ERROR] D:\ws9494\xxx_G\plugins\xx.xxxx.xxx.xxx.xxx\src-gen\at\xxx\xxx\xxx\xxx\xxx\xxx\Scalar.java:[78] 
[ERROR] 	if (value.getActualInstance() instanceof BigDecimal) {
[ERROR] 	                                         ^^^^^^^^^^
[ERROR] BigDecimal cannot be resolved to a type
[ERROR] D:\ws9494\xxx_G\plugins\xx.xxxx.xxx.xxx.xxx\src-gen\at\xxx\xxx\xxx\xxx\xxx\xxx\Scalar.java:[79] 
[ERROR] 	JsonObject obj = adapterBigDecimal.toJsonTree((BigDecimal)value.getActualInstance()).getAsJsonObject();
[ERROR] 	                                               ^^^^^^^^^^
...
[ERROR] BigDecimal cannot be resolved to a type
[ERROR] D:\ws9494\xxx_G\plugins\xx.xxxx.xxx.xxx.xxx\src-gen\at\xxx\xxx\xxx\xxx\xxx\xxx\Scalar.java:[282] 
[ERROR] 	BigDecimal.validateJsonObject(jsonObj);
[ERROR] 	^^^^^^^^^^
[ERROR] BigDecimal cannot be resolved
[ERROR] D:\ws9494\xxx_G\plugins\xx.xxxx.xxx.xxx.xxx\src-gen\at\xxx\xxx\xxx\xxx\xxx\xxx\Scalar.java:[290] 
[ERROR] 	Boolean.validateJsonObject(jsonObj);
[ERROR] 	        ^^^^^^^^^^^^^^^^^^
...                            ^^^^^
[ERROR] Array cannot be resolved to a type
[ERROR] D:\ws9494\xxx_G\plugins\xx.xxxx.xxx.xxx.xxx\src-gen\at\xxx\xxx\xxx\xxx\xxx\xxx\Value.java:[80] 
[ERROR] 	JsonObject obj = adapterArray.toJsonTree((Array)value.getActualInstance()).getAsJsonObject();
[ERROR] 	                                          ^^^^^
[ERROR] Array cannot be resolved to a type
[ERROR] D:\ws9494\xxx_G\plugins\xx.xxxx.xxx.xxx.xxx\src-gen\at\xxx\xxx\xxx\xxx\xxx\xxx\Value.java:[107] 
[ERROR] 	Array.validateJsonObject(jsonObject);
...
[ERROR] Array cannot be resolved
[ERROR] 30 problems (30 errors)

R4NN4 avatar Jun 17 '22 14:06 R4NN4

Description

openapi-generator-maven-plugin release version 5.1.0

A Java client, generated with openapi-generator-maven-plugin will throw a com.google.gson.JsonSyntaxException during JSON deserialization of a server response:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 44 path $[0].value
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
	at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
	at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
	at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
	at com.google.gson.Gson.fromJson(Gson.java:932)
	at com.google.gson.Gson.fromJson(Gson.java:897)
	at com.google.gson.Gson.fromJson(Gson.java:846)

Server response JSON:

[{"value":1000.0}]

Minimal example spec:

openapi: 3.0.0
info:
  description: Minimal Spec
  version: 1.0.0
  title: Test
servers:
- url: test
  description: Service name
paths:
  "/read":
    post:
      operationId: readList
      description: Read list of values
      responses:
        '200':
          "$ref": "#/components/responses/ValueList"
components:
  responses:
    ValueList:
      description: List of value list objects
      content:
        application/json:
          schema:
            "$ref": "#/components/schemas/ValueList"
  schemas:
    ValueList:
      description: Array of value objects
      type: array
      items:
        "$ref": "#/components/schemas/ValueObject"
    ValueObject:
      type: object
      description: A value object
      required:
      - value
      properties:
        value:
          "$ref": "#/components/schemas/Value"
    Value:
      description: Value datatype
      oneOf:
      - "$ref": "#/components/schemas/Scalar"
      - "$ref": "#/components/schemas/Array"
    Scalar:
      description: Scalar datatype
      oneOf:
      - type: string
        maxLength: 1089
      - type: number
      - type: boolean
    Array:
      type: array
      minItems: 1
      items:
        "$ref": "#/components/schemas/Scalar"

Because of the exception further processing is interrupted.

openapi-generator version

openapi-generator-maven-plugin release version 5.1.0

Generation Details

Following maven pom.xml is used to generate the Java Client:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

         <build>
               <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-clean-plugin</artifactId>
				<version>3.1.0</version>
				<configuration>
					<filesets>
						<fileset>
							<directory>src-gen</directory>
							<includes>
								<include>**/*</include>
							</includes>
						</fileset>
					</filesets>
				</configuration>
			      </plugin>

                              <plugin>
				<groupId>org.openapitools</groupId>
				<artifactId>openapi-generator-maven-plugin</artifactId>
				<!-- RELEASE_VERSION -->
				<version>5.1.0</version>
				<!-- /RELEASE_VERSION -->
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<inputSpec>http://.../api.yaml</inputSpec>
							<generatorName>java</generatorName>
							<output>${project.basedir}</output>
							<generateModelTests>false</generateModelTests>
							<generateApiTests>false</generateApiTests>
							<packageName>at.internal.test</packageName>
							<apiPackage>at.internal.test</apiPackage>
							<modelPackage>at.internal.test.model</modelPackage>
							<invokerPackage>at.internal.test.invoker</invokerPackage>
							<withXml>false</withXml>
							<configOptions>
								<sourceFolder>src-gen</sourceFolder>
							</configOptions>
							<verbose>true</verbose>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
Steps to reproduce
  • Generate Java client and server with example spec
  • Generate client request / server response
  • Server should response with JSON
  • Exception will be thrown on client
Related issues/PRs

--

Suggest a fix

--

Bug Report Checklist

Is this issue finally resolved?

shifpeng avatar Jun 30 '22 09:06 shifpeng

+1 "Is this issue finally resolved?"

aruvic avatar Oct 12 '22 16:10 aruvic

@wing328 Hi William, please can you prioritize this bug for 6.2.1. I'm trying to read the code and fix it myself but the code base is huge and I still don't get how things work :-)

aruvic avatar Oct 19 '22 07:10 aruvic

I used https://github.com/OpenAPITools/openapi-generator/pull/13897 and can confirm that it fixed this OneOf Issue for me. @aruvic maybe you can try using that PR, would be interesting to know if it also helps you.

bdznkr avatar Mar 21 '23 15:03 bdznkr

merged the fix. sorry for taking a bit long to get it merged as there are too many PRs.

wing328 avatar Jun 29 '23 08:06 wing328