Skip to content

Commit

Permalink
feat: @JsonAnySetter/Getter Jackson annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
memdal committed Sep 23, 2024
1 parent 9b31cf4 commit 37a9595
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/generators/java/presets/JacksonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ export const JAVA_JACKSON_PRESET: JavaPreset = {
//Properties that are dictionaries with unwrapped options, cannot get the annotation because it cannot be accurately unwrapped by the jackson library.
const isDictionary =
property.property instanceof ConstrainedDictionaryModel;
const isAdditionalProperties =
property.propertyName === 'additionalProperties';
const hasUnwrappedOptions =
isDictionary &&
(property.property as ConstrainedDictionaryModel).serializationType ===
'unwrap';

const blocks: string[] = [];

if (isAdditionalProperties) {
blocks.push(renderer.renderAnnotation('JsonAnySetter'));
}

if (hasUnwrappedOptions) {
if (!property.required) {
blocks.push(
Expand Down Expand Up @@ -67,6 +73,16 @@ export const JAVA_JACKSON_PRESET: JavaPreset = {

blocks.push(content);

return renderer.renderBlock(blocks);
},
getter({ renderer, property, content }) {
const isAdditionalProperties =
property.propertyName === 'additionalProperties';
const blocks: string[] = [];
if (isAdditionalProperties) {
blocks.push(renderer.renderAnnotation('JsonAnyGetter'));
}
blocks.push(content);
return renderer.renderBlock(blocks);
}
},
Expand Down
26 changes: 26 additions & 0 deletions test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface Pet {
@JsonProperty(\\"test\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private TestAllOf test;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -50,6 +51,7 @@ public interface Pet {
public TestAllOf getTest() { return this.test; }
public void setTest(TestAllOf test) { this.test = test; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -164,6 +166,7 @@ public interface Pet {
@JsonProperty(\\"testProp2\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String testProp2;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -173,6 +176,7 @@ public interface Pet {
public String getTestProp2() { return this.testProp2; }
public void setTestProp2(String testProp2) { this.testProp2 = testProp2; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -220,12 +224,14 @@ public interface Pet {
@JsonProperty(\\"testProp\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String testProp;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public String getTestProp() { return this.testProp; }
public void setTestProp(String testProp) { this.testProp = testProp; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -287,6 +293,7 @@ public interface Pet {
@JsonProperty(\\"test\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private Test test;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -305,6 +312,7 @@ public interface Pet {
public Test getTest() { return this.test; }
public void setTest(Test test) { this.test = test; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -420,6 +428,7 @@ Array [
@JsonProperty(\\"event_time\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private java.time.OffsetDateTime eventTime;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -429,6 +438,7 @@ Array [
public java.time.OffsetDateTime getEventTime() { return this.eventTime; }
public void setEventTime(java.time.OffsetDateTime eventTime) { this.eventTime = eventTime; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -536,6 +546,7 @@ public interface Pet {
@JsonProperty(\\"time\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private java.time.OffsetDateTime time;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -555,6 +566,7 @@ public interface Pet {
public java.time.OffsetDateTime getTime() { return this.time; }
public void setTime(java.time.OffsetDateTime time) { this.time = time; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -654,6 +666,7 @@ public interface Pet {
@JsonProperty(\\"time\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private java.time.OffsetDateTime time;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -673,6 +686,7 @@ public interface Pet {
public java.time.OffsetDateTime getTime() { return this.time; }
public void setTime(java.time.OffsetDateTime time) { this.time = time; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -736,6 +750,7 @@ Array [
@NotNull
@JsonProperty(\\"type\\")
private final CloudEventType type = CloudEventType.DOG;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -744,6 +759,7 @@ Array [
public CloudEventType getType() { return this.type; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -836,11 +852,13 @@ public interface Pet {
@NotNull
@JsonProperty(\\"type\\")
private final DogType type = DogType.DOG;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public DogType getType() { return this.type; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -915,11 +933,13 @@ public interface Pet {
@NotNull
@JsonProperty(\\"type\\")
private final CatType type = CatType.CAT;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public CatType getType() { return this.type; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -1010,12 +1030,14 @@ public interface Vehicle {
@JsonProperty(\\"vehicle\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private Vehicle vehicle;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public Vehicle getVehicle() { return this.vehicle; }
public void setVehicle(Vehicle vehicle) { this.vehicle = vehicle; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -1061,11 +1083,13 @@ public interface Vehicle {
@NotNull
@JsonProperty(\\"vehicleType\\")
private final VehicleType vehicleType = VehicleType.CAR;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public VehicleType getVehicleType() { return this.vehicleType; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -1140,11 +1164,13 @@ public interface Vehicle {
@NotNull
@JsonProperty(\\"vehicleType\\")
private final VehicleType vehicleType = VehicleType.TRUCK;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public VehicleType getVehicleType() { return this.vehicleType; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down

0 comments on commit 37a9595

Please sign in to comment.