micronaut-projects / micronaut-gradle-plugin

A Gradle Plugin for Micronaut
Apache License 2.0
67 stars 44 forks source link

Openapi plugin for client: cannot add custom annotation to models properly #1054

Open tomgeek27 opened 3 days ago

tomgeek27 commented 3 days ago

Expected Behavior

I'd like to use this configuration:

micronaut {
  runtime("tomcat")
  testRuntime("junit5")
  // ...
  openapi {
    client(file) {
      // other config
      additionalModelTypeAnnotations = [
          "@lombok.Builder(toBuilder = true)"
      ]
    }
  }

to generate models with just the @lombok.Builder(toBuilder = true) annotation

Actual Behaviour

The problem relies on the fact that it add automatically other lombok annotation (even if I define lombok = false) as:

and this implies that will create a conflict between the constructor generated by @AllArgsConstructor and the one generated by the plugin itself

Steps To Reproduce

No response

Environment Information

No response

Example Application

No response

Version

id("io.micronaut.openapi") version '4.4.4

tomgeek27 commented 3 days ago

In general I'd like to use a more user friendly way to build model. At the moment the only way is using a constructor with every attributes (become crazy with 10+ params)

tomgeek27 commented 2 days ago

🗞️ UPDATE: for the moment I found this workaround that could be useful for whoever is struggling with the same problem:

micronaut {
  // ...
  openapi {
    client(file) {
      // other config
      additionalProperties = [
        requiredPropertiesInConstructor: 'false',
      ]
    }
  }
}

With this configuration you can create any model with default empty constructor and then define every named attributes in this way:

  Model model = new Model().attrA(..).attrB(..);
altro3 commented 2 hours ago

Actually, I deliberately didn't add the @Builder annotation because, in my opinion, it's pointless. Instead, I added the @Accessors(chain = true) annotation just so that you could use the builder principle, but not create extra objects (builders)

And if you enable lombok option like this:

micronaut {
  // ...
  openapi {
    client(file) {
      lombok = true
    }
  }
}

You can create objects like this:

var model = new Model()
    .setAttrA(..)
    .setAttrB(..);