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

torand / jsonschema2java / 18399814123

10 Oct 2025 07:35AM UTC coverage: 78.214% (-0.9%) from 79.157%
18399814123

push

github

torand
fix: bean validation annotations on primitive subtypes of compound pojo property types now generated

287 of 413 branches covered (69.49%)

Branch coverage included in aggregate %.

440 of 533 new or added lines in 26 files covered. (82.55%)

1 existing line in 1 file now uncovered.

808 of 987 relevant lines covered (81.86%)

4.75 hits per line

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

84.21
/src/main/java/io/github/torand/jsonschema2java/collectors/PropertyInfoCollector.java
1
/*
2
 * Copyright (c) 2024-2025 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!
NEW
56
            AnnotationInfo jsonPropAnnotation = getJsonPropertyAnnotation(name);
×
NEW
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 (propertyType.isDeprecated()) {
3✔
85
            schemaParams.add("deprecated = true");
4✔
86
        }
87

88
        return new AnnotationInfo(
9✔
89
            "@Schema(%s)".formatted(joinCsv(schemaParams)),
5✔
90
            "org.eclipse.microprofile.openapi.annotations.media.Schema"
91
        );
92
    }
93

94
    private AnnotationInfo getJsonPropertyAnnotation(String name) {
NEW
95
        return new AnnotationInfo(
×
NEW
96
            "@JsonProperty(\"%s\")".formatted(name),
×
97
            "com.fasterxml.jackson.annotation.JsonProperty"
98
        );
99
    }
100
}
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