fix: authorization values in Java modules (#20644)

This commit is contained in:
Simone Dalcastagné 2025-03-02 16:39:37 +01:00 committed by GitHub
parent 85ab65c4ca
commit 70df1170cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View File

@ -22,9 +22,11 @@ import io.airlift.airline.Option;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.AuthorizationValue;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.apache.commons.text.WordUtils;
import org.openapitools.codegen.auth.AuthParser;
import org.openapitools.codegen.validation.ValidationResult;
import org.openapitools.codegen.validations.oas.OpenApiEvaluator;
import org.openapitools.codegen.validations.oas.RuleConfiguration;
@ -44,12 +46,20 @@ public class Validate extends OpenApiGeneratorCommand {
@Option(name = { "--recommend"}, title = "recommend spec improvements")
private Boolean recommend;
@Option(
name = {"-a", "--auth"},
title = "authorization",
description = "adds authorization headers when fetching the OpenAPI definitions remotely. "
+ "Pass in a URL-encoded string of name:header with a comma separating multiple values")
private String auth;
@Override
public void execute() {
System.out.println("Validating spec (" + spec + ")");
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIParser().readLocation(spec, null, options);
final List<AuthorizationValue> authorizationValues = AuthParser.parse(this.auth);
SwaggerParseResult result = new OpenAPIParser().readLocation(spec, authorizationValues, options);
List<String> messageList = result.getMessages();
Set<String> errors = new HashSet<>(messageList);
Set<String> warnings = new HashSet<>();

View File

@ -24,6 +24,7 @@ import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.parser.OpenAPIResolver;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.AuthorizationValue;
import io.swagger.v3.parser.core.models.ParseOptions;
import lombok.Setter;
import org.apache.commons.io.FileUtils;
@ -37,6 +38,7 @@ import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.openapitools.codegen.*;
import org.openapitools.codegen.auth.AuthParser;
import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.config.MergedSpecBuilder;
@ -1002,8 +1004,10 @@ public class CodeGenMojo extends AbstractMojo {
parseOptions.setResolve(true);
final URL remoteUrl = inputSpecRemoteUrl();
final List<AuthorizationValue> authorizationValues = AuthParser.parse(this.auth);
return Hashing.sha256().hashBytes(
new OpenAPIParser().readLocation(remoteUrl == null ? inputSpec : remoteUrl.toString(), null, parseOptions)
new OpenAPIParser().readLocation(remoteUrl == null ? inputSpec : remoteUrl.toString(), authorizationValues, parseOptions)
.getOpenAPI().toString().getBytes(StandardCharsets.UTF_8)
).toString();
}
@ -1089,7 +1093,9 @@ public class CodeGenMojo extends AbstractMojo {
// Merge the OpenAPI spec file.
final var parseOptions = new ParseOptions();
parseOptions.setResolve(true);
final var openApiMerged = new OpenAPIResolver(new OpenAPIV3Parser().readLocation(inputSpec, null, parseOptions).getOpenAPI()).resolve();
final List<AuthorizationValue> authorizationValues = AuthParser.parse(this.auth);
final var openApiMerged = new OpenAPIResolver(new OpenAPIV3Parser().readLocation(inputSpec, authorizationValues, parseOptions).getOpenAPI()).resolve();
// Switch based on JSON or YAML.
final var extension = inputSpec.toLowerCase(Locale.ROOT).endsWith(".json") ? ".json" : ".yaml";