add lombok model support on spring
Merged
DA
3y ago+5737+5737+0123456789012345678901234567890123456789-5-5-0123456789
There are several issues raised to request generate model with lombok support, include #5447, #10841, #14156, #16250, #17606
This PR add lombok support on spring project, i plan to first implement it on spring then port to all other java projects.
When user add lombok annotation by additionalModelTypeAnnotations,
java -jar openapi-generator-cli.jar generate -g spring \
-i petstore.json \
--additional-properties=hideGenerationTimestamp=true,additionalModelTypeAnnotations=@lombok.Data;@lombok.NoArgsConstructor;@lombok.AllArgsConstructor
will generate below code, clean with just field definations.
/**
* Tag
*/
@lombok.Data
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Tag {
private Long id;
private String name;
}
User can combine the lombok annotations to customize the output model
java -jar openapi-generator-cli.jar generate -g spring \
-i petstore.json \
--additional-properties=hideGenerationTimestamp=true,additionalModelTypeAnnotations=@lombok.RequiredArgsConstructor;@lombok.NoArgsConstructor;@lombok.AllArgsConstructor
/**
* Tag
*/
@lombok.RequiredArgsConstructor
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Tag {
private Long id;
private String name;
public Tag id(Long id) {
this.id = id;
return this;
}
/**
* Get id
* @return id
*/
@Schema(name = "id", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Tag name(String name) {
this.name = name;
return this;
}
/**
* Get name
* @return name
*/
@Schema(name = "name", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Tag tag = (Tag) o;
return Objects.equals(this.id, tag.id) &&
Objects.equals(this.name, tag.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Tag {\n");
sb.append(" id: ").append(toIndentedString(id)).append("\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
Note
This implementation will discard the x-setter-extra-annotation in setter function and swagger @Schema in geter function.
PR checklist
- Read the contribution guidelines.
- Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- Run the following to build the project and update samples:
(For Windows users, please run the script in Git BASH)./mvnw clean package ./bin/generate-samples.sh ./bin/configs/*.yaml ./bin/utils/export_docs_generators.sh
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - File the PR against the correct branch:
master(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.