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

torand / openapi2java / 27619408421

16 Jun 2026 01:01PM UTC coverage: 84.049% (-0.4%) from 84.496%
27619408421

push

github

torand
fix: transform id or name into valid Java/Kotlin identifier

590 of 831 branches covered (71.0%)

Branch coverage included in aggregate %.

44 of 51 new or added lines in 3 files covered. (86.27%)

1739 of 1940 relevant lines covered (89.64%)

5.33 hits per line

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

87.01
/src/main/java/io/github/torand/openapi2java/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.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.PropertyInfo;
21
import io.github.torand.openapi2java.model.TypeInfo;
22
import io.github.torand.openapi2java.utils.OpenApi2JavaException;
23
import io.swagger.v3.oas.models.media.Schema;
24

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

28
import static io.github.torand.javacommons.lang.StringHelper.isBlank;
29
import static io.github.torand.openapi2java.utils.IdentifierUtils.toJavaIdentifier;
30
import static io.github.torand.openapi2java.utils.StringUtils.escape;
31
import static io.github.torand.openapi2java.utils.StringUtils.joinCsv;
32
import static java.lang.Boolean.TRUE;
33
import static java.util.Objects.nonNull;
34

35
/**
36
 * Collects information about a property from a schema.
37
 */
38
public class PropertyInfoCollector extends BaseCollector {
39
    private final TypeInfoCollector typeInfoCollector;
40

41
    public PropertyInfoCollector(SchemaResolver schemaResolver, Options opts) {
42
        super(opts);
3✔
43
        this.typeInfoCollector = new TypeInfoCollector(schemaResolver, opts);
7✔
44
    }
1✔
45

46
    public PropertyInfo getPropertyInfo(String name, Schema<?> property, boolean required) {
47
        PropertyInfo propInfo = new PropertyInfo(toPropertyName(name))
7✔
48
            .withRequired(required);
2✔
49

50
        var nullabilityResolution = required
2✔
51
            ? TypeInfoCollector.NullabilityResolution.FROM_SCHEMA
2✔
52
            : TypeInfoCollector.NullabilityResolution.FORCE_NULLABLE;
2✔
53
        propInfo = propInfo.withType(typeInfoCollector.getTypeInfo(property, nullabilityResolution));
8✔
54

55
        if (opts.addMpOpenApiAnnotations()) {
4!
56
            AnnotationInfo schemaAnnotation = getSchemaAnnotation(property, propInfo.type());
6✔
57
            propInfo = propInfo.withAddedAnnotation(schemaAnnotation);
4✔
58
        }
59

60
        if (opts.addJsonPropertyAnnotations()) {
4!
61
            AnnotationInfo jsonPropAnnotation = getJsonPropertyAnnotation(name);
4✔
62
            propInfo = propInfo.withAddedAnnotation(jsonPropAnnotation);
4✔
63
        }
64

65
        if (TRUE.equals(property.getDeprecated())) {
5✔
66
            propInfo = propInfo.withDeprecationMessage(formatDeprecationMessage(property.getExtensions()));
7✔
67
        }
68

69
        return propInfo;
2✔
70
    }
71

72
    private String toPropertyName(String propertyName) {
73
        if (isBlank(propertyName)) {
3!
NEW
74
            throw new OpenApi2JavaException("Blank property name not allowed");
×
75
        }
76

77
        String pojoPropertyName = toJavaIdentifier(propertyName);
3✔
78
        if (isBlank(pojoPropertyName)) {
3!
NEW
79
            throw new OpenApi2JavaException("Property name '%s' can't be transformed into a valid %s property name".formatted(propertyName, opts.useKotlinSyntax() ? "Kotlin" : "Java"));
×
80
        }
81

82
        return pojoPropertyName;
2✔
83
    }
84

85
    private AnnotationInfo getSchemaAnnotation(Schema<?> property, TypeInfo typeInfo) {
86
        boolean required = !typeInfoCollector.isNullable(property) && !typeInfo.nullable();
12✔
87

88
        List<String> schemaParams = new ArrayList<>();
4✔
89
        schemaParams.add("description = \"%s\"".formatted(normalizeDescription(property.getDescription())));
14✔
90
        if (required) {
2✔
91
            schemaParams.add("required = true");
4✔
92
        }
93
        if (nonNull(property.getDefault())) {
4!
94
            schemaParams.add("defaultValue = \"%s\"".formatted(property.getDefault().toString()));
×
95
        }
96
        if (nonNull(typeInfo.schemaFormat())) {
4✔
97
            schemaParams.add("format = \"%s\"".formatted(typeInfo.schemaFormat()));
12✔
98
        }
99
        if (nonNull(typeInfo.schemaPattern())) {
4✔
100
            schemaParams.add("pattern = \"%s\"".formatted(escape(typeInfo.schemaPattern())));
13✔
101
        }
102
        if (nonNull(typeInfo.schemaMinLength())) {
4✔
103
            schemaParams.add("minLength = %d".formatted(typeInfo.schemaMinLength()));
12✔
104
        }
105
        if (nonNull(typeInfo.schemaMaxLength())) {
4✔
106
            schemaParams.add("maxLength = %d".formatted(typeInfo.schemaMaxLength()));
12✔
107
        }
108
        if (TRUE.equals(property.getDeprecated())) {
5✔
109
            schemaParams.add("deprecated = true");
4✔
110
        }
111

112
        return new AnnotationInfo(
9✔
113
            "@Schema(%s)".formatted(joinCsv(schemaParams)),
5✔
114
            "org.eclipse.microprofile.openapi.annotations.media.Schema"
115
        );
116
    }
117

118
    private AnnotationInfo getJsonPropertyAnnotation(String name) {
119
        return new AnnotationInfo(
10✔
120
            "@JsonProperty(\"%s\")".formatted(name),
3✔
121
            "com.fasterxml.jackson.annotation.JsonProperty"
122
        );
123
    }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc