OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.51k stars 6.27k forks source link

[REQ][JAVA]: Add @com.fasterxml.jackson.annotation.JsonProperty when @lombok.Data is used #18794

Open nokhod opened 4 weeks ago

nokhod commented 4 weeks ago
Description

When using @lombok.Data in additionalModelTypeAnnotations, the getters for fields are not generated. While @com.fasterxml.jackson.annotation.JsonProperty is only placed on getter methods, this causes a problem when the property name is in kebab-case rather than the default camel-case.

openapi-generator version

7.6.0

OpenAPI declaration file content or URL

pojo.mustache

Command line used for generation

Regular maven build.

Steps to reproduce

Configuring the maven plugin using the following configOptions:

<configOptions>
    <library>spring-cloud</library>
    <useSpringBoot3>true</useSpringBoot3>
    <useResponseEntity>false</useResponseEntity>
    <bigDeciamlAsString>true</bigDeciamlAsString>
    <dateLibrary>java8-localdatetime</dateLibrary>
    <useEnumCaseInsensitive>true</useEnumCaseInsensitive>
    <configPackage>com.kbakhtiari</configPackage>
    <generatedConstructorWithRequiredArgs>false</generatedConstructorWithRequiredArgs>
    <additionalModelTypeAnnotations>
        @lombok.Data
        @lombok.NoArgsConstructor
        @lombok.AllArgsConstructor
        @lombok.Builder(toBuilder = true)    
    </additionalModelTypeAnnotations>
</configOptions>
Related issues/PRs

N/A

Suggest a fix/enhancement

Adding the following code snippet to pojo.mustache:

{{#lombok.Data}}
@JsonProperty("{{baseName}}")
{{/lombok.Data}}
dabdirb commented 3 weeks ago

@nokhod, @Jsonproperty can put on field, not only getter method, you just need set x-field-extra-annotation, for example the User object in OpenAPI spec 3.0

    User:
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 10
        username:
          type: string
          example: theUser
        firstName:
          type: string
          example: John
          x-field-extra-annotation: |-
            @JsonProperty("first_name")
        lastName:
          type: string
          example: James
          x-field-extra-annotation: |-
            @JsonProperty("last_name")          
        email:
          type: string
          example: john@email.com
        password:
          type: string
          example: '12345'
        phone:
          type: string
          example: '12345'
        userStatus:
          type: integer
          description: User Status
          format: int32
          example: 1
      xml:
        name: user

the generated model is

/**
 * User
 */
@lombok.Data
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
@lombok.Builder(toBuilder = true)

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.7.0-SNAPSHOT")
public class User {

  private Long id;

  private String username;

  @JsonProperty("first_name")
  private String firstName;

  @JsonProperty("last_name")          
  private String lastName;

  private String email;

  private String password;

  private String phone;

  private Integer userStatus;

}
nokhod commented 3 weeks ago

Hi @dabdirb Unfortunately, the openapi specs are not in my control to alter.