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

zalando / problem / #2625

24 Feb 2023 06:50AM CUT coverage: 100.0%. Remained the same
#2625

push

web-flow
Merge pull request #406 from zalando/AlexanderYastrebov-patch-1

CODEOWNERS: remove @AlexanderYastrebov

375 of 375 relevant lines covered (100.0%)

1.0 hits per line

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

100.0
/jackson-datatype-problem/src/main/java/org/zalando/problem/jackson/ProblemModule.java
1
package org.zalando.problem.jackson;
2

3
import com.fasterxml.jackson.core.Version;
4
import com.fasterxml.jackson.core.util.VersionUtil;
5
import com.fasterxml.jackson.databind.Module;
6
import com.fasterxml.jackson.databind.module.SimpleModule;
7
import org.apiguardian.api.API;
8
import org.zalando.problem.DefaultProblem;
9
import org.zalando.problem.Exceptional;
10
import org.zalando.problem.Problem;
11
import org.zalando.problem.Status;
12
import org.zalando.problem.StatusType;
13

14
import java.util.Collections;
15
import java.util.HashMap;
16
import java.util.Map;
17

18
import static org.apiguardian.api.API.Status.STABLE;
19

20
@API(status = STABLE)
21
public final class ProblemModule extends Module {
22

23
    private final boolean stackTraces;
24
    private final Map<Integer, StatusType> statuses;
25

26
    /**
27
     * TODO document
28
     *
29
     * @see Status
30
     */
31
    public ProblemModule() {
32
        this(Status.class);
1✔
33
    }
1✔
34

35
    /**
36
     * TODO document
37
     *
38
     * @param <E> generic enum type
39
     * @param types status type enums
40
     * @throws IllegalArgumentException if there are duplicate status codes across all status types
41
     */
42
    @SafeVarargs
43
    public <E extends Enum<?> & StatusType> ProblemModule(final Class<? extends E>... types)
44
            throws IllegalArgumentException {
45

46
        this(false, buildIndex(types));
1✔
47
    }
1✔
48

49
    private ProblemModule(final boolean stackTraces, final Map<Integer, StatusType> statuses) {
1✔
50
        this.stackTraces = stackTraces;
1✔
51
        this.statuses = statuses;
1✔
52
    }
1✔
53

54

55
    @Override
56
    public String getModuleName() {
57
        return ProblemModule.class.getSimpleName();
1✔
58
    }
59

60
    @SuppressWarnings("deprecation")
61
    @Override
62
    public Version version() {
63
        return VersionUtil.mavenVersionFor(ProblemModule.class.getClassLoader(),
1✔
64
                "org.zalando", "jackson-datatype-problem");
65
    }
66

67
    @Override
68
    public void setupModule(final SetupContext context) {
69
        final SimpleModule module = new SimpleModule();
1✔
70

71
        module.setMixInAnnotation(Exceptional.class, stackTraces ?
1✔
72
                ExceptionalMixin.class :
73
                ExceptionalWithoutStacktraceMixin.class);
74

75
        module.setMixInAnnotation(DefaultProblem.class, AbstractThrowableProblemMixIn.class);
1✔
76
        module.setMixInAnnotation(Problem.class, ProblemMixIn.class);
1✔
77

78
        module.addSerializer(StatusType.class, new StatusTypeSerializer());
1✔
79
        module.addDeserializer(StatusType.class, new StatusTypeDeserializer(statuses));
1✔
80

81
        module.setupModule(context);
1✔
82
    }
1✔
83

84
    @SafeVarargs
85
    private static <E extends Enum<?> & StatusType> Map<Integer, StatusType> buildIndex(
86
            final Class<? extends E>... types) {
87
        final Map<Integer, StatusType> index = new HashMap<>();
1✔
88

89
        for (final Class<? extends E> type : types) {
1✔
90
            for (final E status : type.getEnumConstants()) {
1✔
91
                if (index.containsKey(status.getStatusCode())) {
1✔
92
                    throw new IllegalArgumentException("Duplicate status codes are not allowed");
1✔
93
                }
94
                index.put(status.getStatusCode(), status);
1✔
95
            }
96
        }
97

98
        return Collections.unmodifiableMap(index);
1✔
99
    }
100

101
    public ProblemModule withStackTraces() {
102
        return withStackTraces(true);
1✔
103
    }
104

105
    public ProblemModule withStackTraces(final boolean stackTraces) {
106
        return new ProblemModule(stackTraces, statuses);
1✔
107
    }
108

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