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

torand / openapi2java / 18202480060

02 Oct 2025 06:44PM UTC coverage: 81.561% (-0.8%) from 82.388%
18202480060

push

github

web-flow
Merge pull request #48 from torand/refactor-info-classes

Refactor info classes

507 of 736 branches covered (68.89%)

Branch coverage included in aggregate %.

825 of 934 new or added lines in 38 files covered. (88.33%)

11 existing lines in 7 files now uncovered.

1541 of 1775 relevant lines covered (86.82%)

5.09 hits per line

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

86.96
/src/main/java/io/github/torand/openapi2java/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.openapi2java.collectors;
17

18
import io.github.torand.openapi2java.generators.Options;
19
import io.github.torand.openapi2java.model.AnnotationInfo;
20
import io.github.torand.openapi2java.model.PojoInfo;
21
import io.github.torand.openapi2java.model.PropertyInfo;
22
import io.swagger.v3.oas.models.media.Schema;
23

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

28
import static io.github.torand.javacommons.collection.CollectionHelper.nonEmpty;
29
import static io.github.torand.javacommons.lang.StringHelper.nonBlank;
30
import static io.github.torand.openapi2java.collectors.Extensions.EXT_MODEL_SUBDIR;
31
import static io.github.torand.openapi2java.collectors.Extensions.extensions;
32
import static io.github.torand.openapi2java.utils.StringUtils.joinCsv;
33
import static java.lang.Boolean.TRUE;
34

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

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

48
    public PojoInfo getPojoInfo(String name, Schema<?> schema) {
49
        PojoInfo pojoInfo = new PojoInfo(name);
5✔
50

51
        Optional<String> maybeModelSubdir = extensions(schema.getExtensions()).getString(EXT_MODEL_SUBDIR);
6✔
52
        pojoInfo = pojoInfo.withModelSubdir(maybeModelSubdir.orElse(null))
9✔
53
            .withModelSubpackage(maybeModelSubdir.map(this::dirPath2PackagePath).orElse(null));
6✔
54

55
        if (opts.addMpOpenApiAnnotations()) {
4!
56
            pojoInfo = pojoInfo.withAddedAnnotation(getSchemaAnnotation(name, schema));
7✔
57
        }
58

59
        if (TRUE.equals(schema.getDeprecated())) {
5!
NEW
60
            pojoInfo = pojoInfo.withDeprecationMessage(formatDeprecationMessage(schema.getExtensions()));
×
61
        }
62

63
        pojoInfo = pojoInfo.withAddedProperties(getSchemaProperties(schema));
6✔
64

65
        return pojoInfo;
2✔
66
    }
67

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

71
        schemaParams.add("name = \"%s\"".formatted(modelName2SchemaName(name)));
13✔
72
        schemaParams.add("description = \"%s\"".formatted(normalizeDescription(pojo.getDescription())));
14✔
73
        if (TRUE.equals(pojo.getDeprecated())) {
5!
74
            schemaParams.add("deprecated = true");
×
75
        }
76

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

83
    private List<PropertyInfo> getSchemaProperties(Schema<?> schema) {
84
        List<PropertyInfo> props = new ArrayList<>();
4✔
85

86
        if (nonEmpty(schema.getAllOf())) {
4✔
87
            schema.getAllOf().forEach(subSchema -> props.addAll(getSchemaProperties(subSchema)));
14✔
88
        } else if (nonBlank(schema.get$ref())) {
4✔
89
            Schema<?> $refSchema = schemaResolver.getOrThrow(schema.get$ref());
6✔
90
            return getSchemaProperties($refSchema);
4✔
91
        } else {
92
            schema.getProperties().forEach((propName, propSchema) -> {
7✔
93
                props.add(propertyInfoCollector.getPropertyInfo(propName, propSchema, isRequired(schema, propName)));
12✔
94
            });
1✔
95
        }
96

97
        return props;
2✔
98
    }
99

100
    private boolean isRequired(Schema<?> schema, String propName) {
101
        return nonEmpty(schema.getRequired()) && schema.getRequired().contains(propName);
13!
102
    }
103
}
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