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

Bynder / bynder-java-sdk / 10272101318

06 Aug 2024 06:37PM UTC coverage: 40.281% (+1.1%) from 39.218%
10272101318

push

github

web-flow
Support for Custom Metaproperties (#126)

* fix testcases for windows, remove deprecated initMocks in favour of openMocks, add gitignore to prevent target/ files to be checked into git

* replace other initMocks with openMocks calls

* implement custom JsonDeserializer to support "property_" prefixed custom metadata

---------

Co-authored-by: Jonas Dickel <jonas.dickel@cec.valantic.com>

26 of 27 new or added lines in 3 files covered. (96.3%)

773 of 1919 relevant lines covered (40.28%)

0.4 hits per line

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

96.0
/src/main/java/com/bynder/sdk/util/MediaTypeAdapter.java
1
package com.bynder.sdk.util;
2

3
import java.lang.reflect.Field;
4
import java.lang.reflect.Type;
5
import java.util.ArrayList;
6
import java.util.LinkedHashMap;
7
import java.util.List;
8
import java.util.Map;
9

10
import com.bynder.sdk.model.Media;
11
import com.google.gson.Gson;
12
import com.google.gson.GsonBuilder;
13
import com.google.gson.JsonDeserializationContext;
14
import com.google.gson.JsonDeserializer;
15
import com.google.gson.JsonElement;
16
import com.google.gson.JsonObject;
17
import com.google.gson.JsonParseException;
18

19
public class MediaTypeAdapter implements JsonDeserializer<Media> {
20

21
    private static final String PROPERTY_PREFIX = "property_";
22
    private static final String CUSTOM_METAPROPERTY_FIELDNAME = "customMetaproperties";
23

24
    private final Gson gson;
25

26
    public MediaTypeAdapter() {
1✔
27
        this.gson = new GsonBuilder().registerTypeAdapter(Boolean.class, new BooleanTypeAdapter()).create();
1✔
28
    }
1✔
29

30
    @Override
31
    public Media deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
32
            throws JsonParseException {
33
        JsonObject jsonObject = json.getAsJsonObject();
1✔
34
        Media media = gson.fromJson(jsonObject, Media.class);
1✔
35

36
        Map<String, List<String>> metaproperties = new LinkedHashMap<>();
1✔
37

38
        for (Map.Entry<String, JsonElement> elementJson : jsonObject.entrySet()) {
1✔
39
            if (elementJson.getKey().startsWith(PROPERTY_PREFIX)) {
1✔
40
                String propertyName = elementJson.getKey().substring(PROPERTY_PREFIX.length());
1✔
41
                List<String> values = metaproperties.getOrDefault(metaproperties, new ArrayList<>());
1✔
42
                if (elementJson.getValue().isJsonArray()) {
1✔
43
                    for (JsonElement element : elementJson.getValue().getAsJsonArray()) {
1✔
44
                        values.add(element.getAsString());
1✔
45
                    }
1✔
46
                } else {
47
                    values.add(elementJson.getValue().getAsString());
1✔
48
                }
49
                metaproperties.put(propertyName, values);
1✔
50
            }
51
        }
1✔
52
        setMetaproperties(media, metaproperties);
1✔
53
        return media;
1✔
54
    }
55

56
    private void setMetaproperties(Media media, Map<String, List<String>> metaproperties) {
57
        try {
58
            Field metapropertiesField = media.getClass().getDeclaredField(CUSTOM_METAPROPERTY_FIELDNAME);
1✔
59
            metapropertiesField.setAccessible(true);
1✔
60
            metapropertiesField.set(media, metaproperties);
1✔
NEW
61
        } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
×
62
            // does not occur unless Media class is changed
63
        }
1✔
64
    }
1✔
65

66
}
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