• 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

75.0
/src/main/java/io/github/torand/jsonschema2java/collectors/PojoInfoCollector.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.PojoInfo;
21
import io.github.torand.jsonschema2java.model.PropertyInfo;
22
import io.github.torand.jsonschema2java.utils.JsonSchemaDef;
23

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

28
import static io.github.torand.jsonschema2java.collectors.Extensions.EXT_MODEL_SUBDIR;
29
import static io.github.torand.jsonschema2java.utils.StringUtils.joinCsv;
30
import static java.util.Objects.nonNull;
31

32
/**
33
 * Collects information about a pojo from a schema.
34
 */
35
public class PojoInfoCollector extends BaseCollector {
36
    private final PropertyInfoCollector propertyInfoCollector;
37
    private final SchemaResolver schemaResolver;
38

39
    public PojoInfoCollector(Options opts, SchemaResolver schemaResolver) {
40
        super(opts);
3✔
41
        this.schemaResolver = schemaResolver;
3✔
42
        this.propertyInfoCollector = new PropertyInfoCollector(opts, schemaResolver);
7✔
43
    }
1✔
44

45
    public PojoInfo getPojoInfo(String name, JsonSchemaDef schema) {
46
        PojoInfo pojoInfo = new PojoInfo(name);
5✔
47

48
        Optional<String> maybeModelSubdir = schema.extensions().getString(EXT_MODEL_SUBDIR);
5✔
49
        pojoInfo = pojoInfo.withModelSubdir(maybeModelSubdir.orElse(null))
9✔
50
            .withModelSubpackage(maybeModelSubdir.map(this::dirPath2PackagePath).orElse(null));
6✔
51

52
        if (opts.addMpOpenApiAnnotations()) {
4!
53
            pojoInfo = pojoInfo.withAddedAnnotation(getSchemaAnnotation(name, schema));
7✔
54
        }
55

56
        if (schema.isDeprecated()) {
3!
NEW
57
            pojoInfo = pojoInfo.withDeprecationMessage(formatDeprecationMessage(schema.extensions()));
×
58
        }
59

60
        pojoInfo = pojoInfo.withAddedProperties(getSchemaProperties(schema));
6✔
61

62
        return pojoInfo;
2✔
63
    }
64

65
    private AnnotationInfo getSchemaAnnotation(String name, JsonSchemaDef pojo) {
66
        List<String> schemaParams = new ArrayList<>();
4✔
67

68
        schemaParams.add("name = \"%s\"".formatted(modelName2SchemaName(name)));
13✔
69
        schemaParams.add("description = \"%s\"".formatted(normalizeDescription(pojo.description())));
14✔
70
        if (pojo.isDeprecated()) {
3!
71
            schemaParams.add("deprecated = true");
×
72
        }
73

74
        return new AnnotationInfo(
9✔
75
            "@Schema(%s)".formatted(joinCsv(schemaParams)),
5✔
76
            "org.eclipse.microprofile.openapi.annotations.media.Schema"
77
        );
78
    }
79

80
    private List<PropertyInfo> getSchemaProperties(JsonSchemaDef schema) {
81
        List<PropertyInfo> props = new ArrayList<>();
4✔
82

83
        if (schema.hasAllOf()) {
3!
84
            schema.allOf().forEach(subSchema -> props.addAll(getSchemaProperties(subSchema)));
×
85
        } else if (nonNull(schema.ref())) {
4!
NEW
86
            JsonSchemaDef refSchema = schemaResolver.getOrThrow(schema.ref());
×
NEW
87
            return getSchemaProperties(refSchema);
×
88
        } else {
89
            schema.properties().forEach((propName, propSchema) ->
7✔
90
                props.add(propertyInfoCollector.getPropertyInfo(propName, propSchema, schema.isRequired(propName)))
12✔
91
            );
92
        }
93

94
        return props;
2✔
95
    }
96
}
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