Skip to content

Open-API: Fix compilation errors in generated Java classes due to mismatched return types - #11806

Closed
VladimirYushkevich wants to merge 2 commits into
apache:mainfrom
VladimirYushkevich:openapi-contentenum-fix
Closed

VladimirYushkevich wants to merge 2 commits into
apache:mainfrom
VladimirYushkevich:openapi-contentenum-fix

Conversation

@VladimirYushkevich

Copy link
Copy Markdown

Description
This PR addresses a compilation issue in the generated java classes from rest-catalog-open-api.yaml specification.

Problem
The generated Java classes fail to compile due to incorrect return types for the getAction() and getContent() methods. While the parent classes expect these methods to return a String, the child classes return specific ActionEnum and ContentEnum types defined within each child class.

Solution
To resolve this, ActionEnum and ContentEnum types are now defined globally in the OpenAPI specification. Both the parent and child classes reference these global types, ensuring compatibility and eliminating the compile-time errors.

Steps to Reproduce the Issue
Create a .openapi-generator-ignore file with the following content:

src/main/java/org/openapitools/client/ProgressResponseBody.java  

Generate Java classes using the OpenAPI generator Docker image:

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
-i /local/rest-catalog-open-api.yaml \
-g java \
-o /local/build/openapi \
--library jersey3  

Navigate to the generated stub:

cd open-api/build/openapi 

Add the missing Swagger dependency to build.gradle:

implementation 'io.swagger.core.v3:swagger-annotations:2.1.11' 

Run the Gradle build:

chmod -R 755 gradlew  
./gradlew build 

Observe the compilation errors, such as:

iceberg/open-api/build/openapi/src/main/java/org/openapitools/client/model/SetDefaultSortOrderUpdate.java:105: error: getAction() in SetDefaultSortOrderUpdate cannot override getAction() in BaseUpdate
  public ActionEnum getAction() {
                   ^
  return type ActionEnum is not compatible with String  
...
iceberg/open-api/build/openapi/src/main/java/org/openapitools/client/model/EqualityDeleteFile.java:109: error: getContent() in EqualityDeleteFile cannot override getContent() in ContentFile
  public ContentEnum getContent() {
                    ^
  return type ContentEnum is not compatible with String  
...

…matched return types

In this PR:
- Defined ActionEnum and ContentEnum as global types in rest-catalog-open-api.yaml.
- Updated parent and child classes to reference these global enum types, ensuring compatibility.
Comment thread open-api/rest-catalog-open-api.py Outdated
)


class ContentFile(BaseModel):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we edit the existing class instead of removing and adding it in the down? That way the diff will be minimal for review.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is autogenerated, I moved ContentEnum and ActionEnum in proper place to minimize diff a bit.

Comment thread open-api/rest-catalog-open-api.yaml Outdated

ActionEnum:
type: string
enum:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we keep it as the same order of declaration, like set and reset together for all the operations?


class DataFile(ContentFile):
content: Literal['data']
content: ContentEnum

@ajantha-bhat ajantha-bhat Dec 23, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a public interface change for people depending on this file.
Any guidelines on this? cc: @Fokko, @nastra

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this file is just here for test purposes and to see what kind of code changes the changes in the yaml produces. With the open-API spec, it can sometimes be hard to see what the impact is.

@ajantha-bhat ajantha-bhat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Minor nit about Enum members ordering, we can take a call based on what others think.

cc: @nastra, @Fokko


class UpgradeFormatVersionUpdate(BaseUpdate):
action: Literal['upgrade-format-version']
action: ActionEnum

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a Pydantics' perspective this has a different semantic meaning. Before, we required that the action field for the UpgradeFormatVersionUpdate needs to be upgrade-format-version, now it can be any value from the enum. But, this can also be a limitation of the Pydantic open-api generator. Let me take a deeper look.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the Jersey one, it is the other way around, it will generate an ActionEnum per Update:

public enum ActionEnum {
  ADD_SPEC(String.valueOf("add-spec"));

  private String value;

  ActionEnum(String value) {
    this.value = value;
  }

  @JsonValue
  public String getValue() {
    return value;
  }

  @Override
  public String toString() {
    return String.valueOf(value);
  }

  @JsonCreator
  public static ActionEnum fromValue(String value) {
    for (ActionEnum b : ActionEnum.values()) {
      if (b.value.equals(value)) {
        return b;
      }
    }
    throw new IllegalArgumentException("Unexpected value '" + value + "'");
  }
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VladimirYushkevich @ajantha-bhat

I've been playing around with alternative solutions. In Open-API there are many ways to get to the same result. What about #11955? The issue is related to the Enum, and with Open-API 3.1 the const is added looks more appropriate here. LMKWYT

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fokko if #11955 fixes java compilation issues I'm fine with that. I can close this PR but need to verify my generated java classes.

Fokko added a commit to Fokko/iceberg that referenced this pull request Jan 13, 2025
Fokko added a commit to Fokko/iceberg that referenced this pull request Jan 13, 2025
Fokko added a commit to Fokko/iceberg that referenced this pull request Jan 13, 2025
Fokko added a commit that referenced this pull request Jan 14, 2025
@Fokko

Fokko commented Feb 5, 2025

Copy link
Copy Markdown
Contributor

@VladimirYushkevich Can you confirm that updating the Open-API version has fixed the issue?

@VladimirYushkevich

VladimirYushkevich commented Feb 6, 2025 •

Copy link
Copy Markdown
Author

@VladimirYushkevich Can you confirm that updating the Open-API version has fixed the issue?

@Fokko, I have reviewed the issue I mentioned in description and found that it is no longer present. Thank you for the fix; we can proceed to close this pull request.

However, in our production codebase, we are utilizing the openapi-generator-maven-plugin, and the Maven build failed with the following error:

[ERROR] 
org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
 | Error count: 7, Warning count: 109
Errors: 
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN]
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.swagger.v3.oas.models.media.JsonSchema["properties"]->java.util.LinkedHashMap["properties"])
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.swagger.v3.oas.models.media.JsonSchema["properties"]->java.util.LinkedHashMap["updates"])
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.swagger.v3.oas.models.media.JsonSchema["properties"]->java.util.LinkedHashMap["config"])
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.swagger.v3.oas.models.media.JsonSchema["properties"]->java.util.LinkedHashMap["metadata"])
	-attribute info.license.identifier is missing
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.swagger.v3.oas.models.media.JsonSchema["properties"]->java.util.LinkedHashMap["summary"])
Warnings: 
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN]
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.swagger.v3.oas.models.media.JsonSchema["properties"]->java.util.LinkedHashMap["properties"])
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.swagger.v3.oas.models.media.JsonSchema["properties"]->java.util.LinkedHashMap["updates"])
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.swagger.v3.oas.models.media.JsonSchema["properties"]->java.util.LinkedHashMap["config"])
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.swagger.v3.oas.models.media.JsonSchema["properties"]->java.util.LinkedHashMap["metadata"])
	-attribute info.license.identifier is missing
	-Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.swagger.v3.oas.models.media.JsonSchema["properties"]->java.util.LinkedHashMap["summary"])

Unfortunately I have no time to dig what is the source of failure, just noticed from the stacktrace:

Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.Boolean` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from (MismatchedInputException.java:59)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch (DeserializationContext.java:1746)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken (DeserializationContext.java:1520)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken (DeserializationContext.java:1425)
    at com.fasterxml.jackson.databind.DeserializationContext.extractScalarFromObject (DeserializationContext.java:937)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseBoolean (StdDeserializer.java:517)
    at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$BooleanDeserializer.deserialize (NumberDeserializers.java:227)
    at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$BooleanDeserializer.deserialize (NumberDeserializers.java:200)
    at com.fasterxml.jackson.databind.ObjectMapper._convert (ObjectMapper.java:4444)
    at com.fasterxml.jackson.databind.ObjectMapper.convertValue (ObjectMapper.java:4380)
    at io.swagger.v3.core.util.ModelDeserializer.deserializeJsonSchema (ModelDeserializer.java:157)
    at io.swagger.v3.core.util.ModelDeserializer.deserialize (ModelDeserializer.java:44)
    at io.swagger.v3.core.util.ModelDeserializer.deserialize (ModelDeserializer.java:33)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap (MapDeserializer.java:623)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize (MapDeserializer.java:449)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize (MapDeserializer.java:32)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet (MethodProperty.java:129)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize (BeanDeserializer.java:314)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize (BeanDeserializer.java:177)
    at com.fasterxml.jackson.databind.ObjectMapper._convert (ObjectMapper.java:4444)
    at com.fasterxml.jackson.databind.ObjectMapper.convertValue (ObjectMapper.java:4380)
    at io.swagger.v3.core.util.ModelDeserializer.deserializeJsonSchema (ModelDeserializer.java:142)
    at io.swagger.v3.core.util.ModelDeserializer.deserialize (ModelDeserializer.java:44)
    at io.swagger.v3.core.util.ModelDeserializer.deserialize (ModelDeserializer.java:33)
    at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue (DefaultDeserializationContext.java:323)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose (ObjectMapper.java:4730)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue (ObjectMapper.java:3677)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue (ObjectMapper.java:3645)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.deepcopy (OpenAPI31Traverser.java:922)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseSchema (OpenAPI31Traverser.java:914)
    at io.swagger.v3.parser.reference.ReferenceVisitor.resolveSchemaRef (ReferenceVisitor.java:233)
    at io.swagger.v3.parser.reference.ReferenceVisitor.visitSchema (ReferenceVisitor.java:120)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseSchema (OpenAPI31Traverser.java:790)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseMediaType (OpenAPI31Traverser.java:603)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseMap (OpenAPI31Traverser.java:933)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseResponse (OpenAPI31Traverser.java:299)
    at io.swagger.v3.parser.reference.ReferenceVisitor.resolveRef (ReferenceVisitor.java:181)
    at io.swagger.v3.parser.reference.ReferenceVisitor.visitResponse (ReferenceVisitor.java:131)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseResponse (OpenAPI31Traverser.java:289)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseMap (OpenAPI31Traverser.java:933)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseResponses (OpenAPI31Traverser.java:270)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseOperation (OpenAPI31Traverser.java:237)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traversePathItem (OpenAPI31Traverser.java:424)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseMap (OpenAPI31Traverser.java:933)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traversePaths (OpenAPI31Traverser.java:197)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverseOpenApi (OpenAPI31Traverser.java:124)
    at io.swagger.v3.parser.reference.OpenAPI31Traverser.traverse (OpenAPI31Traverser.java:65)
    at io.swagger.v3.parser.reference.OpenAPIDereferencer31.dereference (OpenAPIDereferencer31.java:74)
    at io.swagger.v3.parser.OpenAPIV3Parser.resolve (OpenAPIV3Parser.java:221)
    at io.swagger.v3.parser.OpenAPIV3Parser.readContents (OpenAPIV3Parser.java:178)
    at io.swagger.v3.parser.OpenAPIV3Parser.readLocation (OpenAPIV3Parser.java:94)
    at io.swagger.parser.OpenAPIParser.readLocation (OpenAPIParser.java:16)
    at org.openapitools.codegen.config.CodegenConfigurator.toContext (CodegenConfigurator.java:589)
    at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput (CodegenConfigurator.java:647)
    at org.openapitools.codegen.plugin.CodeGenMojo.execute (CodeGenMojo.java:815)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:255)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:201)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:361)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:314)
    at org.codehaus.classworlds.Launcher.main (Launcher.java:41)

looks like io.swagger.v3.parser.reference.OpenAPI31Traverser has issues

@VladimirYushkevich

Copy link
Copy Markdown
Author

UPD: after adding <skipValidateSpec>true</skipValidateSpec> into openapi-generator-maven-plugin having:
incompatible types: java.lang.String cannot be converted to com.dremio.enterprise.dremiocatalog.icebergclient.model.PlanStatus compilation error in EmptyPlanningResult class.

@github-actions

github-actions Bot commented Mar 9, 2025

Copy link
Copy Markdown

This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the [email protected] list. Thank you for your contributions.

@github-actions github-actions Bot added the stale label Mar 9, 2025
@github-actions

Copy link
Copy Markdown

This pull request has been closed due to lack of activity. This is not a judgement on the merit of the PR in any way. It is just a way of keeping the PR queue manageable. If you think that is incorrect, or the pull request requires review, you can revive the PR at any time.

@github-actions github-actions Bot closed this Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants