Skip to content

[BUG] [Java/Kotlin] type string with format as 'uri' does not compile #3160

Closed
@rainerh

Description

@rainerh
Description

When generating a Java/Kotlin client with a string format uri, the resulting code does not compile. There is no conversion from String to URI:

    type:
      type: string
      format: uri
      default: 'about:blank'

results in:

  @JsonProperty("type")
  private URI type = "about:blank";
openapi-generator version

3.3.4
4.0.1

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Sample API
  license:
    name: MIT
servers:
  - url: http://example.com
paths:
  /nothing:
    post:
      operationId: nothing
      responses:
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "https://opensource.zalando.com/problem/schema.yaml#/Problem"
Command line used for generation

Java Server Side:

    generatorName = "spring"
    configOptions = [delegatePattern: "true", title: "myApi"]

Java Client:

    generatorName = "java"
    additionalProperties = [
        hideGenerationTimestamp: true
    ]
    configOptions = [
        library: "resttemplate"
    ]

Kotlin Client:

    generatorName = "kotlin"
    configOptions = [
        dateLibrary: "java8"
    ]
    systemProperties = [
        modelDocs: "false"
    ]
Steps to reproduce
  1. run openapi-generator generate -i sample.yaml -g spring with the above sample as input.
  2. mvn compile produces:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/hermanns/code/test/bla/src/main/java/org/openapitools/model/Problem.java:[20,22] incompatible types: java.lang.String cannot be converted to java.net.URI
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.559 s
[INFO] Finished at: 2019-06-17T13:29:16+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project openapi-spring: Compilation failure
[ERROR] /Users/hermanns/code/test/bla/src/main/java/org/openapitools/model/Problem.java:[20,22] incompatible types: java.lang.String cannot be converted to java.net.URI
  1. open src/main/java/org/openapitools/model/Problem.java
  2. URI type is generated as: private URI type = "about:blank";
Related issues/PRs
Suggest a fix/enhancement

Add special format handling for String with 'uri' format

diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
index 0ef33cf048..f1b035cf74 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
@@ -784,6 +784,11 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
                 return p.getDefault().toString();
             }
             return null;
+        } else if (ModelUtils.isURISchema(p)) {
+            if (p.getDefault() != null) {
+                return "URI.create(\"" + escapeText((String) p.getDefault()) + "\")";
+            }
+            return null;
         } else if (ModelUtils.isStringSchema(p)) {
             if (p.getDefault() != null) {
                 String _default = (String) p.getDefault();
diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
index 58d22e5165..438ce57def 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
@@ -834,6 +834,10 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
             if (p.getDefault() != null) {
                 return p.getDefault().toString();
             }
+        } else if (ModelUtils.isURISchema(p)) {
+            if (p.getDefault() != null) {
+                return "URI.create('" + p.getDefault() + "')";
+            }
         } else if (ModelUtils.isStringSchema(p)) {
             if (p.getDefault() != null) {
                 return "'" + p.getDefault() + "'";

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions