Skip to content

Commit feb0c94

Browse files
authored
fix: serialize rotation as number if possible (#7544)
* fix: serialize rotation as number if possible * add test
1 parent 19d8a5f commit feb0c94

3 files changed

Lines changed: 99 additions & 0 deletions

File tree

‎vaadin-charts-flow-parent/vaadin-charts-flow/src/main/java/com/vaadin/flow/component/charts/model/Labels.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99
package com.vaadin.flow.component.charts.model;
1010

11+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
12+
import com.vaadin.flow.component.charts.model.serializers.LabelsRotationSerializer;
1113
import com.vaadin.flow.component.charts.model.style.Style;
1214

1315
public class Labels extends AbstractConfigurationObject {
@@ -325,6 +327,7 @@ public void setZIndex(Number zIndex) {
325327
this.zIndex = zIndex;
326328
}
327329

330+
@JsonSerialize(using = LabelsRotationSerializer.class)
328331
public String getRotation() {
329332
return rotation;
330333
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2000-2025 Vaadin Ltd.
3+
*
4+
* This program is available under Vaadin Commercial License and Service Terms.
5+
*
6+
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full
7+
* license.
8+
*/
9+
package com.vaadin.flow.component.charts.model.serializers;
10+
11+
import java.io.IOException;
12+
13+
import com.fasterxml.jackson.core.JsonGenerator;
14+
import com.fasterxml.jackson.databind.JsonSerializer;
15+
import com.fasterxml.jackson.databind.SerializerProvider;
16+
import com.vaadin.flow.component.charts.model.Labels;
17+
18+
/**
19+
* Serializer for {@link Labels#getRotation()}. Attempts to serialize the
20+
* rotation value as a number, otherwise falls back to writing it as a string to
21+
* support values like "auto".
22+
*/
23+
public class LabelsRotationSerializer extends JsonSerializer<String> {
24+
@Override
25+
public void serialize(String value, JsonGenerator gen,
26+
SerializerProvider serializers) throws IOException {
27+
if (value == null) {
28+
return;
29+
}
30+
31+
try {
32+
double d = Double.parseDouble(value);
33+
gen.writeNumber(d);
34+
} catch (NumberFormatException e) {
35+
gen.writeString(value);
36+
}
37+
}
38+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2000-2025 Vaadin Ltd.
3+
*
4+
* This program is available under Vaadin Commercial License and Service Terms.
5+
*
6+
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full
7+
* license.
8+
*/
9+
package com.vaadin.flow.component.charts;
10+
11+
import org.junit.Assert;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
import com.fasterxml.jackson.core.JsonProcessingException;
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import com.vaadin.flow.component.charts.model.Labels;
18+
import com.vaadin.flow.component.charts.util.ChartSerialization;
19+
20+
public class LabelsSerializationTest {
21+
22+
private ObjectMapper objectMapper;
23+
24+
@Before
25+
public void setUp() {
26+
objectMapper = ChartSerialization.createObjectMapper();
27+
}
28+
29+
@Test
30+
public void testRotation_isParsableAsNumber_serializedAsNumber()
31+
throws JsonProcessingException {
32+
Labels labels = new Labels();
33+
labels.setRotation("90");
34+
String json = objectMapper.writeValueAsString(labels);
35+
Assert.assertTrue("Rotation should be serialized as a number",
36+
json.contains("\"rotation\":90.0"));
37+
}
38+
39+
@Test
40+
public void testRotation_isNotParsableAsNumber_serializedAsString()
41+
throws JsonProcessingException {
42+
Labels labels = new Labels();
43+
labels.setRotation("auto");
44+
String json = objectMapper.writeValueAsString(labels);
45+
Assert.assertTrue("Rotation should be serialized as a string",
46+
json.contains("\"rotation\":\"auto\""));
47+
}
48+
49+
@Test
50+
public void testRotation_isNull_notSerialized()
51+
throws JsonProcessingException {
52+
Labels labels = new Labels();
53+
labels.setRotation((String) null);
54+
String json = objectMapper.writeValueAsString(labels);
55+
Assert.assertFalse("Rotation should not be serialized",
56+
json.contains("\"rotation\""));
57+
}
58+
}

0 commit comments

Comments
 (0)