Skip to content

Commit 67a4ba4

Browse files
SimoneDalkaJacobOJ
authored andcommitted
fix: authorization values in Java modules (#20644)
1 parent adfce30 commit 67a4ba4

File tree

2 files changed

+19
-3
lines changed
  • modules
    • openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd
    • openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin

2 files changed

+19
-3
lines changed

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Validate.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222

2323
import io.swagger.parser.OpenAPIParser;
2424
import io.swagger.v3.oas.models.OpenAPI;
25+
import io.swagger.v3.parser.core.models.AuthorizationValue;
2526
import io.swagger.v3.parser.core.models.ParseOptions;
2627
import io.swagger.v3.parser.core.models.SwaggerParseResult;
2728
import org.apache.commons.text.WordUtils;
29+
import org.openapitools.codegen.auth.AuthParser;
2830
import org.openapitools.codegen.validation.ValidationResult;
2931
import org.openapitools.codegen.validations.oas.OpenApiEvaluator;
3032
import org.openapitools.codegen.validations.oas.RuleConfiguration;
@@ -44,12 +46,20 @@ public class Validate extends OpenApiGeneratorCommand {
4446
@Option(name = { "--recommend"}, title = "recommend spec improvements")
4547
private Boolean recommend;
4648

49+
@Option(
50+
name = {"-a", "--auth"},
51+
title = "authorization",
52+
description = "adds authorization headers when fetching the OpenAPI definitions remotely. "
53+
+ "Pass in a URL-encoded string of name:header with a comma separating multiple values")
54+
private String auth;
55+
4756
@Override
4857
public void execute() {
4958
System.out.println("Validating spec (" + spec + ")");
5059
ParseOptions options = new ParseOptions();
5160
options.setResolve(true);
52-
SwaggerParseResult result = new OpenAPIParser().readLocation(spec, null, options);
61+
final List<AuthorizationValue> authorizationValues = AuthParser.parse(this.auth);
62+
SwaggerParseResult result = new OpenAPIParser().readLocation(spec, authorizationValues, options);
5363
List<String> messageList = result.getMessages();
5464
Set<String> errors = new HashSet<>(messageList);
5565
Set<String> warnings = new HashSet<>();

modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.swagger.v3.core.util.Yaml;
2525
import io.swagger.v3.parser.OpenAPIResolver;
2626
import io.swagger.v3.parser.OpenAPIV3Parser;
27+
import io.swagger.v3.parser.core.models.AuthorizationValue;
2728
import io.swagger.v3.parser.core.models.ParseOptions;
2829
import lombok.Setter;
2930
import org.apache.commons.io.FileUtils;
@@ -37,6 +38,7 @@
3738
import org.apache.maven.project.MavenProject;
3839
import org.apache.maven.project.MavenProjectHelper;
3940
import org.openapitools.codegen.*;
41+
import org.openapitools.codegen.auth.AuthParser;
4042
import org.openapitools.codegen.config.CodegenConfigurator;
4143
import org.openapitools.codegen.config.GlobalSettings;
4244
import org.openapitools.codegen.config.MergedSpecBuilder;
@@ -1002,8 +1004,10 @@ private String calculateInputSpecHash(String inputSpec) {
10021004
parseOptions.setResolve(true);
10031005

10041006
final URL remoteUrl = inputSpecRemoteUrl();
1007+
final List<AuthorizationValue> authorizationValues = AuthParser.parse(this.auth);
1008+
10051009
return Hashing.sha256().hashBytes(
1006-
new OpenAPIParser().readLocation(remoteUrl == null ? inputSpec : remoteUrl.toString(), null, parseOptions)
1010+
new OpenAPIParser().readLocation(remoteUrl == null ? inputSpec : remoteUrl.toString(), authorizationValues, parseOptions)
10071011
.getOpenAPI().toString().getBytes(StandardCharsets.UTF_8)
10081012
).toString();
10091013
}
@@ -1089,7 +1093,9 @@ private Path createCollapsedSpec() throws MojoExecutionException {
10891093
// Merge the OpenAPI spec file.
10901094
final var parseOptions = new ParseOptions();
10911095
parseOptions.setResolve(true);
1092-
final var openApiMerged = new OpenAPIResolver(new OpenAPIV3Parser().readLocation(inputSpec, null, parseOptions).getOpenAPI()).resolve();
1096+
final List<AuthorizationValue> authorizationValues = AuthParser.parse(this.auth);
1097+
1098+
final var openApiMerged = new OpenAPIResolver(new OpenAPIV3Parser().readLocation(inputSpec, authorizationValues, parseOptions).getOpenAPI()).resolve();
10931099

10941100
// Switch based on JSON or YAML.
10951101
final var extension = inputSpec.toLowerCase(Locale.ROOT).endsWith(".json") ? ".json" : ".yaml";

0 commit comments

Comments
 (0)