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

torand / jsonschema2java / 18509998011

14 Oct 2025 09:01PM UTC coverage: 82.516% (+3.7%) from 78.786%
18509998011

push

github

torand
Merge remote-tracking branch 'origin/main'

* origin/main:
  chore(deps): bump org.jacoco:jacoco-maven-plugin from 0.8.13 to 0.8.14

311 of 417 branches covered (74.58%)

Branch coverage included in aggregate %.

850 of 990 relevant lines covered (85.86%)

4.99 hits per line

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

88.64
/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!
57
            pojoInfo = pojoInfo.withDeprecationMessage(formatDeprecationMessage(schema.extensions()));
×
58
        }
59

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

62
        if (schema.additionalProperties() instanceof JsonSchemaDef) {
4✔
63
            throw new IllegalStateException("Schema-based 'additionalProperties' not supported for Pojos. Please specify this inside a property schema instead.");
5✔
64
        }
65

66
        return pojoInfo;
2✔
67
    }
68

69
    private AnnotationInfo getSchemaAnnotation(String name, JsonSchemaDef pojo) {
70
        List<String> schemaParams = new ArrayList<>();
4✔
71

72
        schemaParams.add("name = \"%s\"".formatted(modelName2SchemaName(name)));
13✔
73
        schemaParams.add("description = \"%s\"".formatted(normalizeDescription(pojo.description())));
14✔
74
        if (pojo.isDeprecated()) {
3!
75
            schemaParams.add("deprecated = true");
×
76
        }
77

78
        return new AnnotationInfo(
9✔
79
            "@Schema(%s)".formatted(joinCsv(schemaParams)),
5✔
80
            "org.eclipse.microprofile.openapi.annotations.media.Schema"
81
        );
82
    }
83

84
    private List<PropertyInfo> getSchemaProperties(JsonSchemaDef schema) {
85
        List<PropertyInfo> props = new ArrayList<>();
4✔
86

87
        if (schema.hasAllOf()) {
3✔
88
            schema.allOf().forEach(subSchema -> props.addAll(getSchemaProperties(subSchema)));
14✔
89
        } else if (nonNull(schema.ref())) {
4✔
90
            JsonSchemaDef refSchema = schemaResolver.getOrThrow(schema.ref());
6✔
91
            return getSchemaProperties(refSchema);
4✔
92
        } else {
93
            schema.properties().forEach((propName, propSchema) ->
7✔
94
                props.add(propertyInfoCollector.getPropertyInfo(propName, propSchema, schema.isRequired(propName)))
12✔
95
            );
96
        }
97

98
        return props;
2✔
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