Skip to content

Commit

Permalink
feat!: add jackson annotations additionalProperties (#2103)
Browse files Browse the repository at this point in the history
  • Loading branch information
memdal authored Oct 8, 2024
1 parent e30832c commit 07f39f1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Array [
@JsonProperty(\\"max_number_prop\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private Double maxNumberProp;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -18,6 +19,7 @@ Array [
public Double getMaxNumberProp() { return this.maxNumberProp; }
public void setMaxNumberProp(Double maxNumberProp) { this.maxNumberProp = maxNumberProp; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
Expand Down
19 changes: 19 additions & 0 deletions src/generators/java/presets/JacksonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const JAVA_JACKSON_PRESET: JavaPreset = {
const blocks: string[] = [];

if (hasUnwrappedOptions) {
blocks.push(renderer.renderAnnotation('JsonAnySetter'));
if (!property.required) {
blocks.push(
renderer.renderAnnotation(
Expand Down Expand Up @@ -67,6 +68,24 @@ export const JAVA_JACKSON_PRESET: JavaPreset = {

blocks.push(content);

return renderer.renderBlock(blocks);
},
getter({ renderer, property, content, model }) {
if (model.options.isExtended) {
return content;
}
//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 hasUnwrappedOptions =
isDictionary &&
(property.property as ConstrainedDictionaryModel).serializationType ===
'unwrap';
const blocks: string[] = [];
if (hasUnwrappedOptions) {
blocks.push(renderer.renderAnnotation('JsonAnyGetter'));
}
blocks.push(content);
return renderer.renderBlock(blocks);
}
},
Expand Down
24 changes: 24 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 @@ -49,6 +50,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 @@ -163,6 +165,7 @@ public interface Pet {
@JsonProperty(\\"test_string\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String testString;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -172,6 +175,7 @@ public interface Pet {
public String getTestString() { return this.testString; }
public void setTestString(String testString) { this.testString = testString; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -264,6 +268,7 @@ public interface Pet {
@JsonProperty(\\"sequence_type\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private CloudEventDotSequenceType sequenceType;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -278,6 +283,7 @@ public interface Pet {
public CloudEventDotSequenceType getSequenceType() { return this.sequenceType; }
public void setSequenceType(CloudEventDotSequenceType sequenceType) { this.sequenceType = sequenceType; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -391,6 +397,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 @@ -400,6 +407,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 @@ -507,6 +515,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 @@ -526,6 +535,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 @@ -625,6 +635,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 @@ -644,6 +655,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 @@ -707,6 +719,7 @@ Array [
@NotNull
@JsonProperty(\\"type\\")
private final CloudEventType type = CloudEventType.DOG;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -715,6 +728,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 @@ -807,11 +821,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 @@ -886,11 +902,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 @@ -981,12 +999,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 @@ -1032,11 +1052,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 @@ -1111,11 +1133,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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Array [
@JsonProperty(\\"extendProp\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String extendProp;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -14,6 +15,7 @@ Array [
@Override
public void setExtendProp(String extendProp) { this.extendProp = extendProp; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
Expand All @@ -31,6 +33,7 @@ exports[`JAVA_JACKSON_PRESET should render Jackson annotations for class 1`] = `
@JsonProperty(\\"max_number_prop\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private Double maxNumberProp;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -40,6 +43,7 @@ exports[`JAVA_JACKSON_PRESET should render Jackson annotations for class 1`] = `
public Double getMaxNumberProp() { return this.maxNumberProp; }
public void setMaxNumberProp(Double maxNumberProp) { this.maxNumberProp = maxNumberProp; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}"
Expand Down Expand Up @@ -97,6 +101,7 @@ public interface Vehicle {
@JsonProperty(\\"name\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String name;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -106,6 +111,7 @@ public interface Vehicle {
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
Expand All @@ -116,6 +122,7 @@ public interface Vehicle {
@JsonProperty(\\"name\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String name;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
Expand All @@ -125,6 +132,7 @@ public interface Vehicle {
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
Expand All @@ -148,25 +156,29 @@ public interface Vehicle {
@JsonProperty(\\"vehicleType\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String vehicleType;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public String getVehicleType() { return this.vehicleType; }
public void setVehicleType(String vehicleType) { this.vehicleType = vehicleType; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
"public class Truck implements Vehicle {
@JsonProperty(\\"vehicleType\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String vehicleType;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public String getVehicleType() { return this.vehicleType; }
public void setVehicleType(String vehicleType) { this.vehicleType = vehicleType; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
Expand All @@ -190,25 +202,29 @@ public interface Vehicle {
@JsonProperty(\\"vehicleType\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String vehicleType;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public String getVehicleType() { return this.vehicleType; }
public void setVehicleType(String vehicleType) { this.vehicleType = vehicleType; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
"public class Truck implements Vehicle {
@JsonProperty(\\"vehicleType\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String vehicleType;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public String getVehicleType() { return this.vehicleType; }
public void setVehicleType(String vehicleType) { this.vehicleType = vehicleType; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
Expand All @@ -232,25 +248,29 @@ public interface Vehicle {
@JsonProperty(\\"passengers\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String passengers;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public String getPassengers() { return this.passengers; }
public void setPassengers(String passengers) { this.passengers = passengers; }
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
"public class Truck implements Vehicle {
@JsonProperty(\\"cargo\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String cargo;
@JsonAnySetter
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, Object> additionalProperties;
public String getCargo() { return this.cargo; }
public void setCargo(String cargo) { this.cargo = cargo; }
@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 07f39f1

Please sign in to comment.