• 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

28.57
/src/main/java/io/github/torand/openapi2java/collectors/OpenApiDefInfoCollector.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.ImportInfo;
21
import io.github.torand.openapi2java.model.OpenApiDefInfo;
22
import io.swagger.v3.oas.models.security.*;
23

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

27
import static io.github.torand.javacommons.collection.CollectionHelper.nonEmpty;
28
import static io.github.torand.javacommons.lang.StringHelper.nonBlank;
29
import static io.github.torand.openapi2java.utils.StringUtils.joinCsv;
30
import static java.util.Objects.nonNull;
31

32
/**
33
 * Collects information about an OpenAPI definition from a collection of security requirements.
34
 */
35
public class OpenApiDefInfoCollector extends BaseCollector {
36
    private final ComponentResolver componentResolver;
37

38
    public OpenApiDefInfoCollector(ComponentResolver componentResolver, Options opts) {
39
        super(opts);
3✔
40
        this.componentResolver = componentResolver;
3✔
41
    }
1✔
42

43
    public OpenApiDefInfo getOpenApiDefInfo(String name, List<SecurityRequirement> securityRequirements) {
44
        OpenApiDefInfo openApiDefInfo = new OpenApiDefInfo(name)
5✔
45
            .withAddedNormalImport("jakarta.ws.rs.core.Application");
2✔
46

47
        if (nonEmpty(securityRequirements)) {
3!
48
            openApiDefInfo = openApiDefInfo.withAddedAnnotation(getSecuritySchemesAnnotation(securityRequirements));
6✔
49
        }
50

51
        return openApiDefInfo;
2✔
52
    }
53

54
    private AnnotationInfo getSecuritySchemesAnnotation(List<SecurityRequirement> securityRequirements) {
55
        List<AnnotationInfo> securitySchemeAnnotations = new ArrayList<>();
4✔
56
        securityRequirements.forEach(sr -> {
5✔
57
            sr.keySet().forEach(schemeName -> {
6✔
58
                securitySchemeAnnotations.add(getSecuritySchemeAnnotation(schemeName));
6✔
59
            });
1✔
60
        });
1✔
61

62
        return new AnnotationInfo(
10✔
63
            "@SecuritySchemes(%s)".formatted(formatAnnotationDefaultParam(securitySchemeAnnotations.stream().map(AnnotationInfo::annotation).toList())),
10✔
64
            "org.eclipse.microprofile.openapi.annotations.security.SecuritySchemes"
65
        ).withAddedImports(securitySchemeAnnotations);
1✔
66
    }
67

68
    private AnnotationInfo getSecuritySchemeAnnotation(String name) {
69
        SecurityScheme securityScheme = componentResolver.securitySchemes().getOrThrow(name);
6✔
70

71
        ImportInfo imports = ImportInfo.empty();
2✔
72
        List<String> params = new ArrayList<>();
4✔
73

74
        params.add("securitySchemeName = \"%s\"".formatted(name));
11✔
75

76
        if (nonBlank(securityScheme.getDescription())) {
4!
77
            params.add("description = \"%s\"".formatted(normalizeDescription(securityScheme.getDescription())));
×
78
        }
79

80
        imports = imports.withAddedNormalImport("org.eclipse.microprofile.openapi.annotations.enums.SecuritySchemeType");
4✔
81
        params.add("type = SecuritySchemeType.%s".formatted(securityScheme.getType().name()));
13✔
82

83
        switch (securityScheme.getType()) {
6!
84
            case APIKEY ->
85
                params.add("name = \"%s\"".formatted(securityScheme.getName()));
×
86

87
            case HTTP -> {
88
                params.add("scheme = \"%s\"".formatted(securityScheme.getScheme()));
×
89
                if (nonNull(securityScheme.getBearerFormat())) {
×
90
                    params.add("bearerFormat = \"%s\"".formatted(securityScheme.getBearerFormat()));
×
91
                }
92
            }
93
            case OAUTH2 -> {
94
                if (nonNull(securityScheme.getFlows())) {
×
NEW
95
                    AnnotationInfo flowsAnnotation = getOAuthFlowsAnnotation(securityScheme.getFlows());
×
NEW
96
                    imports = imports.withAddedImports(flowsAnnotation);
×
NEW
97
                    params.add("flows = %s".formatted(flowsAnnotation.annotation()));
×
UNCOV
98
                }
×
99
            }
100
            case OPENIDCONNECT ->
101
                params.add("openIdConnectUrl = \"%s\"".formatted(securityScheme.getOpenIdConnectUrl()));
13✔
102

103
            case MUTUALTLS ->
UNCOV
104
                throw new IllegalStateException("Security scheme MUTUALTLS not supported");
×
105
        }
106

107
        return new AnnotationInfo(
3✔
108
            (opts.useKotlinSyntax() ? "" : "@") + "SecurityScheme(%s)".formatted(joinCsv(params)),
20✔
109
            "org.eclipse.microprofile.openapi.annotations.security.SecurityScheme"
110
        ).withAddedImports(imports);
1✔
111
    }
112

113
    private AnnotationInfo getOAuthFlowsAnnotation(OAuthFlows flows) {
NEW
114
        ImportInfo imports = ImportInfo.empty();
×
UNCOV
115
        List<String> params = new ArrayList<>();
×
116

117
        if (nonNull(flows.getAuthorizationCode())) {
×
NEW
118
            AnnotationInfo flowAnnotation = getOAuthFlowAnnotation(flows.getAuthorizationCode());
×
NEW
119
            imports = imports.withAddedImports(flowAnnotation);
×
NEW
120
            params.add("authorizationCode = %s".formatted(flowAnnotation.annotation()));
×
121
        }
122
        if (nonNull(flows.getImplicit())) {
×
NEW
123
            AnnotationInfo flowAnnotation = getOAuthFlowAnnotation(flows.getImplicit());
×
NEW
124
            imports = imports.withAddedImports(flowAnnotation);
×
NEW
125
            params.add("implicit = %s".formatted(flowAnnotation.annotation()));
×
126
        }
127
        if (nonNull(flows.getClientCredentials())) {
×
NEW
128
            AnnotationInfo flowAnnotation = getOAuthFlowAnnotation(flows.getClientCredentials());
×
NEW
129
            imports = imports.withAddedImports(flowAnnotation);
×
NEW
130
            params.add("clientCredentials = %s".formatted(flowAnnotation.annotation()));
×
131
        }
132
        if (nonNull(flows.getPassword())) {
×
NEW
133
            AnnotationInfo flowAnnotation = getOAuthFlowAnnotation(flows.getPassword());
×
NEW
134
            imports = imports.withAddedImports(flowAnnotation);
×
NEW
135
            params.add("password = %s".formatted(flowAnnotation.annotation()));
×
136
        }
137

NEW
138
        return new AnnotationInfo(
×
NEW
139
            (opts.useKotlinSyntax() ? "" : "@") + "OAuthFlows(%s)".formatted(joinCsv(params)),
×
140
            "org.eclipse.microprofile.openapi.annotations.security.OAuthFlows"
NEW
141
        ).withAddedImports(imports);
×
142
    }
143

144
    private AnnotationInfo getOAuthFlowAnnotation(OAuthFlow flow) {
NEW
145
        ImportInfo imports = ImportInfo.empty();
×
UNCOV
146
        List<String> params = new ArrayList<>();
×
147

148
        if (nonBlank(flow.getAuthorizationUrl())) {
×
149
            params.add("authorizationUrl = \"%s\"".formatted(flow.getAuthorizationUrl()));
×
150
        }
151
        if (nonBlank(flow.getTokenUrl())) {
×
152
            params.add("tokenUrl = \"%s\"".formatted(flow.getTokenUrl()));
×
153
        }
154
        if (nonBlank(flow.getRefreshUrl())) {
×
155
            params.add("refreshUrl = \"%s\"".formatted(flow.getRefreshUrl()));
×
156
        }
157
        if (nonEmpty(flow.getScopes())) {
×
NEW
158
            List<AnnotationInfo> scopesAnnotations = getScopesAnnotations(flow.getScopes());
×
NEW
159
            imports = imports.withAddedImports(scopesAnnotations);
×
NEW
160
            params.add("scopes = %s".formatted(formatAnnotationNamedParam(scopesAnnotations.stream().map(AnnotationInfo::annotation).toList())));
×
161
        }
162

NEW
163
        return new AnnotationInfo(
×
NEW
164
            (opts.useKotlinSyntax() ? "" : "@") + "OAuthFlow(%s)".formatted(joinCsv(params)),
×
165
            "org.eclipse.microprofile.openapi.annotations.security.OAuthFlow"
NEW
166
        ).withAddedImports(imports);
×
167
    }
168

169
    private List<AnnotationInfo> getScopesAnnotations(Scopes scopes) {
NEW
170
        return scopes.keySet().stream()
×
NEW
171
            .map(name -> getScopeAnnotation(name, scopes.get(name)))
×
UNCOV
172
            .toList();
×
173
    }
174

175
    private AnnotationInfo getScopeAnnotation(String name, String description) {;
NEW
176
        return new AnnotationInfo(
×
NEW
177
            (opts.useKotlinSyntax() ? "" : "@") + "OAuthScope(name = \"%s\", description = \"%s\")".formatted(name, normalizeDescription(description)),
×
178
            "org.eclipse.microprofile.openapi.annotations.security.OAuthScope"
179
        );
180
    }
181
}
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