OpenAPITools / OpenAPITools/openapi-generator

[BUG] Wrong class generation when using ( generateAliasAsModel = True )

Open
#11,872 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

My plugin configuration:

<plugin>
	<groupId>org.openapitools</groupId>
	<artifactId>openapi-generator-maven-plugin</artifactId>
	<version>5.0.0-beta2</version>
	<configuration>
		<enablePostProcessFile>false</enablePostProcessFile>
		<inputSpec>${project.basedir}/src/main/resources/openapi/api.yml</inputSpec>
		<output>${project.build.directory}/openapi-rest/java</output>
		<library>spring-boot</library>
		<generateModels>true</generateModels>
		<generateSupportingFiles>true</generateSupportingFiles>
		<generateAliasAsModel>true</generateAliasAsModel>
		<generatorName>spring</generatorName>

		<generateApiDocumentation>true</generateApiDocumentation>
		<generateModelDocumentation>true</generateModelDocumentation>
		<generateApiTests>true</generateApiTests>
		<configOptions>

			<removeOperationIdPrefix>true</removeOperationIdPrefix>
			<apiNameSuffix>DTO</apiNameSuffix>
			<delegatePattern >true</delegatePattern >
			<generateApi>true</generateApi>
			<skipOperationExample>false</skipOperationExample>
			<java11>true</java11>
			<dateLibrary>java11</dateLibrary>
			<sourceFolder>./</sourceFolder>
			<groupId>com.doppelgr.bmm</groupId>
			<basePackage>com.doppelgr.bmm</basePackage>
			<modelPackage>com.doppelgr.bmm.model</modelPackage>
			<apiPackage>com.doppelgr.bmm.api</apiPackage>
			<configPackage>com.doppelgr.bmm</configPackage>
			<configHelp>true</configHelp>
			<interfaceOnly>true</interfaceOnly>
			<skipDefaultInterface>true</skipDefaultInterface>
		</configOptions>
	</configuration>
	<executions>
		<execution>
			<id>generate-model</id>
			<goals>
				<goal>generate</goal>
			</goals>
		</execution>
	</executions>
</plugin>

The affected openapi schema:

BookmarkDTO:
 type: object
 properties:
  path:
   type: string
  folder:
   type: string
BookmarkListDTO:
 type: array
 items:
  $ref: "#/components/schemas/BookmarkDTO"
BookmarkListPagedDTO:
 type: object
 properties:
  BookmarkList:
   $ref: "#/components/schemas/BookmarkListDTO"
Description

When i exec "mvn clean install" it tries to generate the sources but it fails with

.../BookmarkListPagedDTO.java:[22,46] cannot find symbol
[ERROR]   symbol:   class ArrayList
[ERROR]   location: class com.doppelgr.bmm.model.BookmarkListPagedDTO

And in the generated BookmarkListPagedDTO:

package com.doppelgr.bmm.model;

import java.util.Objects;
import com.doppelgr.bmm.model.BookmarkListDTO;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.openapitools.jackson.nullable.JsonNullable;
import javax.validation.Valid;
import javax.validation.constraints.*;

/**
 * BookmarkListPagedDTO
 */
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-03-14T20:44:32.998659900+01:00[Europe/Paris]")
public class BookmarkListPagedDTO   {
  @JsonProperty("length")
  private Integer length;

  @JsonProperty("BookmarkList")
  private BookmarkListDTO bookmarkList = new ArrayList<BookmarkDTO>();

  public BookmarkListPagedDTO length(Integer length) {
    this.length = length;
    return this;
  }

But i dont think that the problem is the missing import
I think that it should generate this instead:

package com.doppelgr.bmm.model;

import java.util.Objects;
import com.doppelgr.bmm.model.BookmarkListDTO;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.openapitools.jackson.nullable.JsonNullable;
import javax.validation.Valid;
import javax.validation.constraints.*;

/**
* BookmarkListPagedDTO
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-03-14T20:44:32.998659900+01:00[Europe/Paris]")
public class BookmarkListPagedDTO   {
 @JsonProperty("length")
 private Integer length;

 @JsonProperty("BookmarkList")
 private BookmarkListDTO bookmarkList = new BookmarkListDTO();

 public BookmarkListPagedDTO length(Integer length) {
   this.length = length;
   return this;
 }

Using the class constructor instead.

The openapi schema is valid according to https://apitools.dev/swagger-parser/online/

Tested with both 5.0.0-beta2 & 4.3.1

And did some testing with the last 6.0.0 but it looks like the problem persist

package org.openapitools.client.model;

import java.util.Objects;
import java.util.Arrays;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.IOException;
import org.openapitools.client.model.BookmarkListDTO;

/**
 * BookmarkListPagedDTO
 */
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2022-03-14T21:14:07.776609400+01:00[Europe/Paris]")
public class BookmarkListPagedDTO {
  public static final String SERIALIZED_NAME_LENGTH = "length";
  @SerializedName(SERIALIZED_NAME_LENGTH)
  private Integer length;

  public static final String SERIALIZED_NAME_BOOKMARK_LIST = "BookmarkList";
  @SerializedName(SERIALIZED_NAME_BOOKMARK_LIST)
  private BookmarkListDTO bookmarkList = new ArrayList<BookmarkDTO>();


  public BookmarkListPagedDTO length(Integer length) {
    
    this.length = length;
    return this;
  }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the provided BookmarkDTO, BookmarkListDTO, and BookmarkListPagedDTO schema as the reproduction input, then inspect the generated BookmarkListPagedDTO.java for alias-array handling. Compare the BookmarkListDTO field declaration and initializer under the shown Spring Java configuration, and verify that generated sources compile without the ArrayList/BookmarkDTO mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.