• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

smartsheet / smartsheet-java-sdk / #44

25 Aug 2023 05:39PM UTC coverage: 50.55% (+0.1%) from 50.427%
#44

push

github-actions

web-flow
Fix remaining Checkstyle violations and Enable Checkstyle (#58)

* Fix remaining Checkstyle violations and Enable Checkstyle

We are now down to `20` checkstyle violations in main and `0` violations in test.

The remaining 20 violations are not trivial to fix, so I've set checkstyle to allow those 20 violations to exist, but to fail the build if we ever exceed 20 violations. This should make the build fail if any new violations are added.

For tests, we do not allow _any_ violations. This means adding a single violation will fail the build. Once the 20 violations in main are cleaned up, we can make main and test have the same config.

Note: This MR also changes our PR pipeline to run `./gradlew clean build` instead of `./gradlew clean test`. The reason for this is that build runs all the tests and performs all the other checks (such as checkstyle), whereas `test` didn't run checkstyle and we wouldn't have noticed violations until we tried to deploy.

148 of 148 new or added lines in 24 files covered. (100.0%)

3448 of 6821 relevant lines covered (50.55%)

0.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.1
/src/main/java/com/smartsheet/api/internal/json/ObjectValueDeserializer.java
1
package com.smartsheet.api.internal.json;
2

3
/*
4
 * #[license]
5
 * Smartsheet SDK for Java
6
 * %%
7
 * Copyright (C) 2023 Smartsheet
8
 * %%
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *      http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 * %[license]
21
 */
22

23
import com.fasterxml.jackson.core.JsonParser;
24
import com.fasterxml.jackson.core.JsonProcessingException;
25
import com.fasterxml.jackson.core.JsonToken;
26
import com.fasterxml.jackson.databind.DeserializationContext;
27
import com.fasterxml.jackson.databind.DeserializationFeature;
28
import com.fasterxml.jackson.databind.JsonDeserializer;
29
import com.fasterxml.jackson.databind.ObjectMapper;
30
import com.smartsheet.api.models.BooleanObjectValue;
31
import com.smartsheet.api.models.ContactObjectValue;
32
import com.smartsheet.api.models.DateObjectValue;
33
import com.smartsheet.api.models.Duration;
34
import com.smartsheet.api.models.MultiContactObjectValue;
35
import com.smartsheet.api.models.MultiPicklistObjectValue;
36
import com.smartsheet.api.models.NumberObjectValue;
37
import com.smartsheet.api.models.ObjectValue;
38
import com.smartsheet.api.models.Predecessor;
39
import com.smartsheet.api.models.PredecessorList;
40
import com.smartsheet.api.models.StringObjectValue;
41
import com.smartsheet.api.models.enums.ObjectValueType;
42

43
import java.io.IOException;
44
import java.util.ArrayList;
45
import java.util.List;
46

47
public class ObjectValueDeserializer extends JsonDeserializer<ObjectValue> {
1✔
48

49
    @Override
50
    public ObjectValue deserialize(JsonParser jp, DeserializationContext ctxt)
51
            throws IOException, JsonProcessingException {
52

53
        final ObjectValue objectValue;
54
        ContactObjectValue contactObjectValue = null;
1✔
55

56
        if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
1✔
57
            ObjectMapper mapper = new ObjectMapper();
1✔
58
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
1✔
59

60
            ObjectValueAttributeSuperset superset = mapper.readValue(jp, ObjectValueAttributeSuperset.class);
1✔
61

62
            ObjectValueType parsedObjectType;
63
            try {
64
                parsedObjectType = ObjectValueType.valueOf(superset.objectType);
1✔
65
            } catch (IllegalArgumentException e) {
1✔
66
                // If a new object type is introduced to the Smartsheet API that this version of the SDK doesn't support,
67
                // return null instead of throwing an exception.
68
                return null;
1✔
69
            }
1✔
70

71
            switch (parsedObjectType) {
1✔
72
                case DURATION:
73
                    objectValue = new Duration(
1✔
74
                            superset.negative,
75
                            superset.elapsed,
76
                            superset.weeks,
77
                            superset.days,
78
                            superset.hours,
79
                            superset.minutes,
80
                            superset.seconds,
81
                            superset.milliseconds);
82
                    break;
1✔
83

84
                case PREDECESSOR_LIST:
85
                    objectValue = new PredecessorList(superset.predecessors);
×
86
                    break;
×
87

88
                case CONTACT:
89
                    contactObjectValue = new ContactObjectValue();
×
90
                    contactObjectValue.setName(superset.name);
×
91
                    contactObjectValue.setEmail(superset.email);
×
92
                    contactObjectValue.setId(superset.id);
×
93
                    objectValue = contactObjectValue;
×
94
                    break;
×
95

96
                case DATE:
97
                    // Intentional fallthrough
98
                case DATETIME:
99
                    // Intentional fallthrough
100
                case ABSTRACT_DATETIME:
101
                    objectValue = new DateObjectValue(parsedObjectType, superset.value);
1✔
102
                    break;
1✔
103

104
                case MULTI_CONTACT:
105
                    List<ContactObjectValue> contactObjectValues = new ArrayList<>();
×
106
                    for (Object contact: superset.values) {
×
107
                        contactObjectValue = mapper.convertValue(contact, ContactObjectValue.class);
×
108
                        contactObjectValues.add(contactObjectValue);
×
109
                    }
×
110
                    objectValue = new MultiContactObjectValue(contactObjectValues);
×
111
                    break;
×
112

113
                case MULTI_PICKLIST:
114
                    objectValue = new MultiPicklistObjectValue((List<String>) superset.values);
×
115
                    break;
×
116

117
                default:
118
                    objectValue = null;
×
119
            }
120
        } else {
1✔
121
            JsonToken token = jp.getCurrentToken();
1✔
122
            if (token.isBoolean()) {
1✔
123
                objectValue = new BooleanObjectValue(jp.getBooleanValue());
1✔
124
            } else if (token.isNumeric()) {
1✔
125
                objectValue = new NumberObjectValue(jp.getNumberValue());
1✔
126
            } else {
127
                objectValue = new StringObjectValue(jp.getText());
1✔
128
            }
129
        }
130
        return objectValue;
1✔
131
    }
132

133
    private static class ObjectValueAttributeSuperset {
134
        // This needs to be represented as a string so that any new object types added won't completely break the API
135
        public String objectType;
136

137
        // PREDECESSOR_LIST specific attributes
138
        public List<Predecessor> predecessors;
139

140
        // DURATION specific attributes
141
        public Boolean negative;
142
        public Boolean elapsed;
143
        public Double weeks;
144
        public Double days;
145
        public Double hours;
146
        public Double minutes;
147
        public Double seconds;
148
        public Double milliseconds;
149

150
        // CONTACT specific attributes
151
        public String id;
152
        public String name;
153
        public String email;
154

155
        // MULTI_CONTACT
156
        public List<?> values;
157

158
        // Various other types
159
        public String value;
160
    }
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc