Add lombok (or any) annotation to swagger generated class
Asked Answered
S

1

6

I scavenged the internet but didn't find anything to solve this problem. Is it possible to add the lombok annotations in a swagger class UPON generation?

I have this swagger schema:

Product:
type: "object"
required:
  - idSeller
  - model
  - name
  - description
  - price
properties:
  id:
    type: string
  idSeller:
    type: string
  model:
    type: string
  name:
    type: string
  description:
    type: string
  price:
    type: number
    format: currency
    minimum: 0.01

Which generated this code:

@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-18T20:36:31.834-03:00")

public class Product   {
    @JsonProperty("id")
    private String id = null;

    @JsonProperty("idSeller")
    private String idSeller = null;

    //rest of the code ommited
} 

But I need it to generate this:

@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-10-18T20:36:31.834-03:00")

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Product   {
    @JsonProperty("id")
    private String id = null;

    @JsonProperty("idSeller")
    private String idSeller = null;

    //rest of the code ommited
}

With the lombok Data, Builder, NoArgsConstructor and AllArgsConstructor annotations.

Could you help me on this?

Schmid answered 19/10, 2019 at 19:58 Comment(1)
If you use a build tool that allows it, you can write a simple plugin that reads your generated files and adds the annotation to them. I can see this being done very easily with gradleRicardaricardama
P
1

What about

perl -pi -e 's/(?=^public class )/\@Data\n\@Builder\n\@NoArgsConstructor\n\@AllArgsConstructor\n/' *.java

? I guess, that's not what you expected, but you can integrate it into your build process and have a time-proof solution.

Actually, you'll need the imports, too, but that's equally simple.

Picturize answered 20/10, 2019 at 22:37 Comment(2)
I'd suggest an actual java file parser like javaparser which works for all versions of Java thus farRicardaricardama
@Ricardaricardama Sure, yours is a cleaner solution.... mine depends heavily on knowing how the file looks like. I use perl for trivial tasks, which was good enough so far. When something bigger comes, I'll use a proper parser, too.Picturize

© 2022 - 2024 — McMap. All rights reserved.