From 07f39f19b3ef4e2ad55586886fd00795d96cdf40 Mon Sep 17 00:00:00 2001 From: memdal <77838566+memdal@users.noreply.github.com> Date: Tue, 8 Oct 2024 10:46:34 +0200 Subject: [PATCH] feat!: add jackson annotations additionalProperties (#2103) --- .../__snapshots__/index.spec.ts.snap | 2 ++ src/generators/java/presets/JacksonPreset.ts | 19 +++++++++++++++ .../__snapshots__/JavaGenerator.spec.ts.snap | 24 +++++++++++++++++++ .../__snapshots__/JacksonPreset.spec.ts.snap | 20 ++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap b/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap index 1d3f1d6fc0..0a7c33e74e 100644 --- a/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap +++ b/examples/java-generate-jackson-annotation/__snapshots__/index.spec.ts.snap @@ -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 additionalProperties; @@ -18,6 +19,7 @@ Array [ public Double getMaxNumberProp() { return this.maxNumberProp; } public void setMaxNumberProp(Double maxNumberProp) { this.maxNumberProp = maxNumberProp; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }", diff --git a/src/generators/java/presets/JacksonPreset.ts b/src/generators/java/presets/JacksonPreset.ts index f72147b79d..598448b06f 100644 --- a/src/generators/java/presets/JacksonPreset.ts +++ b/src/generators/java/presets/JacksonPreset.ts @@ -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( @@ -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); } }, diff --git a/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap b/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap index f71c1d124b..94bbac3285 100644 --- a/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap +++ b/test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap @@ -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 additionalProperties; @@ -49,6 +50,7 @@ public interface Pet { public TestAllOf getTest() { return this.test; } public void setTest(TestAllOf test) { this.test = test; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -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 additionalProperties; @@ -172,6 +175,7 @@ public interface Pet { public String getTestString() { return this.testString; } public void setTestString(String testString) { this.testString = testString; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -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 additionalProperties; @@ -278,6 +283,7 @@ public interface Pet { public CloudEventDotSequenceType getSequenceType() { return this.sequenceType; } public void setSequenceType(CloudEventDotSequenceType sequenceType) { this.sequenceType = sequenceType; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -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 additionalProperties; @@ -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 getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -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 additionalProperties; @@ -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 getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -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 additionalProperties; @@ -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 getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -707,6 +719,7 @@ Array [ @NotNull @JsonProperty(\\"type\\") private final CloudEventType type = CloudEventType.DOG; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; @@ -715,6 +728,7 @@ Array [ public CloudEventType getType() { return this.type; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -807,11 +821,13 @@ public interface Pet { @NotNull @JsonProperty(\\"type\\") private final DogType type = DogType.DOG; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public DogType getType() { return this.type; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -886,11 +902,13 @@ public interface Pet { @NotNull @JsonProperty(\\"type\\") private final CatType type = CatType.CAT; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public CatType getType() { return this.type; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -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 additionalProperties; public Vehicle getVehicle() { return this.vehicle; } public void setVehicle(Vehicle vehicle) { this.vehicle = vehicle; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -1032,11 +1052,13 @@ public interface Vehicle { @NotNull @JsonProperty(\\"vehicleType\\") private final VehicleType vehicleType = VehicleType.CAR; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public VehicleType getVehicleType() { return this.vehicleType; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } @@ -1111,11 +1133,13 @@ public interface Vehicle { @NotNull @JsonProperty(\\"vehicleType\\") private final VehicleType vehicleType = VehicleType.TRUCK; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public VehicleType getVehicleType() { return this.vehicleType; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } diff --git a/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap b/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap index d4e5689c20..71c27cf5ba 100644 --- a/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap +++ b/test/generators/java/presets/__snapshots__/JacksonPreset.spec.ts.snap @@ -6,6 +6,7 @@ Array [ @JsonProperty(\\"extendProp\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String extendProp; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; @@ -14,6 +15,7 @@ Array [ @Override public void setExtendProp(String extendProp) { this.extendProp = extendProp; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }", @@ -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 additionalProperties; @@ -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 getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }" @@ -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 additionalProperties; @@ -106,6 +111,7 @@ public interface Vehicle { public String getName() { return this.name; } public void setName(String name) { this.name = name; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }", @@ -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 additionalProperties; @@ -125,6 +132,7 @@ public interface Vehicle { public String getName() { return this.name; } public void setName(String name) { this.name = name; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }", @@ -148,12 +156,14 @@ public interface Vehicle { @JsonProperty(\\"vehicleType\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getVehicleType() { return this.vehicleType; } public void setVehicleType(String vehicleType) { this.vehicleType = vehicleType; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }", @@ -161,12 +171,14 @@ public interface Vehicle { @JsonProperty(\\"vehicleType\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getVehicleType() { return this.vehicleType; } public void setVehicleType(String vehicleType) { this.vehicleType = vehicleType; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }", @@ -190,12 +202,14 @@ public interface Vehicle { @JsonProperty(\\"vehicleType\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getVehicleType() { return this.vehicleType; } public void setVehicleType(String vehicleType) { this.vehicleType = vehicleType; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }", @@ -203,12 +217,14 @@ public interface Vehicle { @JsonProperty(\\"vehicleType\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String vehicleType; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getVehicleType() { return this.vehicleType; } public void setVehicleType(String vehicleType) { this.vehicleType = vehicleType; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }", @@ -232,12 +248,14 @@ public interface Vehicle { @JsonProperty(\\"passengers\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String passengers; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getPassengers() { return this.passengers; } public void setPassengers(String passengers) { this.passengers = passengers; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }", @@ -245,12 +263,14 @@ public interface Vehicle { @JsonProperty(\\"cargo\\") @JsonInclude(JsonInclude.Include.NON_NULL) private String cargo; + @JsonAnySetter @JsonInclude(JsonInclude.Include.NON_NULL) private Map additionalProperties; public String getCargo() { return this.cargo; } public void setCargo(String cargo) { this.cargo = cargo; } + @JsonAnyGetter public Map getAdditionalProperties() { return this.additionalProperties; } public void setAdditionalProperties(Map additionalProperties) { this.additionalProperties = additionalProperties; } }",