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

torand / jsonschema2java / 22553968753

01 Mar 2026 10:11PM UTC coverage: 83.689% (+1.3%) from 82.424%
22553968753

push

github

torand
feat: add config parameter 'durationClassName' to specify a custom class to represent string schemas with format "duration" in generated code

328 of 433 branches covered (75.75%)

Branch coverage included in aggregate %.

16 of 16 new or added lines in 5 files covered. (100.0%)

41 existing lines in 6 files now uncovered.

888 of 1020 relevant lines covered (87.06%)

5.25 hits per line

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

87.69
/src/main/java/io/github/torand/jsonschema2java/collectors/PropertyInfoCollector.java
1
/*
2
 * Copyright (c) 2024-2026 Tore Eide Andersen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package io.github.torand.jsonschema2java.collectors;
17

18
import io.github.torand.jsonschema2java.generators.Options;
19
import io.github.torand.jsonschema2java.model.AnnotationInfo;
20
import io.github.torand.jsonschema2java.model.PropertyInfo;
21
import io.github.torand.jsonschema2java.model.TypeInfo;
22
import io.github.torand.jsonschema2java.utils.JsonSchemaDef;
23

24
import java.util.ArrayList;
25
import java.util.List;
26

27
import static io.github.torand.jsonschema2java.utils.StringUtils.joinCsv;
28
import static java.util.Objects.nonNull;
29

30
/**
31
 * Collects information about a property from a schema.
32
 */
33
public class PropertyInfoCollector extends BaseCollector {
34
    private final TypeInfoCollector typeInfoCollector;
35

36
    public PropertyInfoCollector(Options opts, SchemaResolver schemaResolver) {
37
        super(opts);
3✔
38
        this.typeInfoCollector = new TypeInfoCollector(opts, schemaResolver);
7✔
39
    }
1✔
40

41
    public PropertyInfo getPropertyInfo(String name, JsonSchemaDef propertyType, boolean required) {
42
        PropertyInfo propInfo = new PropertyInfo(name)
5✔
43
            .withRequired(required);
2✔
44

45
        var nullabilityResolution = required
2✔
46
            ? TypeInfoCollector.NullabilityResolution.FROM_SCHEMA
2✔
47
            : TypeInfoCollector.NullabilityResolution.FORCE_NULLABLE;
2✔
48
        propInfo = propInfo.withType(typeInfoCollector.getTypeInfo(propertyType, nullabilityResolution));
8✔
49

50
        if (opts.addMpOpenApiAnnotations()) {
4!
51
            AnnotationInfo schemaAnnotation = getSchemaAnnotation(propertyType, propInfo.type());
6✔
52
            propInfo = propInfo.withAddedAnnotation(schemaAnnotation);
4✔
53
        }
54

55
        if (opts.addJsonPropertyAnnotations()) {
4!
56
            AnnotationInfo jsonPropAnnotation = getJsonPropertyAnnotation(name);
×
57
            propInfo = propInfo.withAddedAnnotation(jsonPropAnnotation);
×
58
        }
59

60
        if (propertyType.isDeprecated()) {
3✔
61
            propInfo = propInfo.withDeprecationMessage(formatDeprecationMessage(propertyType.extensions()));
7✔
62
        }
63

64
        return propInfo;
2✔
65
    }
66

67
    private AnnotationInfo getSchemaAnnotation(JsonSchemaDef propertyType, TypeInfo typeInfo) {
68
        boolean required = !typeInfoCollector.isNullable(propertyType) && !typeInfo.nullable();
12✔
69

70
        List<String> schemaParams = new ArrayList<>();
4✔
71
        schemaParams.add("description = \"%s\"".formatted(normalizeDescription(propertyType.description())));
14✔
72
        if (required) {
2✔
73
            schemaParams.add("required = true");
4✔
74
        }
75
        if (nonNull(propertyType.defaultValue())) {
4!
76
            schemaParams.add("defaultValue = \"%s\"".formatted(propertyType.defaultValue()));
×
77
        }
78
        if (nonNull(typeInfo.schemaFormat())) {
4✔
79
            schemaParams.add("format = \"%s\"".formatted(typeInfo.schemaFormat()));
12✔
80
        }
81
        if (nonNull(typeInfo.schemaPattern())) {
4✔
82
            schemaParams.add("pattern = \"%s\"".formatted(typeInfo.schemaPattern()));
12✔
83
        }
84
        if (nonNull(typeInfo.schemaMinLength())) {
4✔
85
            schemaParams.add("minLength = %d".formatted(typeInfo.schemaMinLength()));
12✔
86
        }
87
        if (nonNull(typeInfo.schemaMaxLength())) {
4✔
88
            schemaParams.add("maxLength = %d".formatted(typeInfo.schemaMaxLength()));
12✔
89
        }
90
        if (propertyType.isDeprecated()) {
3✔
91
            schemaParams.add("deprecated = true");
4✔
92
        }
93

94
        return new AnnotationInfo(
9✔
95
            "@Schema(%s)".formatted(joinCsv(schemaParams)),
5✔
96
            "org.eclipse.microprofile.openapi.annotations.media.Schema"
97
        );
98
    }
99

100
    private AnnotationInfo getJsonPropertyAnnotation(String name) {
UNCOV
101
        return new AnnotationInfo(
×
UNCOV
102
            "@JsonProperty(\"%s\")".formatted(name),
×
103
            "com.fasterxml.jackson.annotation.JsonProperty"
104
        );
105
    }
106
}
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

© 2026 Coveralls, Inc