Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Bump PMD with fixes" #26

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ tasks.named('test') {
}

pmd {
toolVersion = '7.5.0'
toolVersion = '6.20.0'
ruleSetConfig = rootProject.resources.text.fromFile('config/pmd/ruleset.xml')
ruleSets = []
ignoreFailures = false
Expand Down
12 changes: 9 additions & 3 deletions config/pmd/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
<exclude-pattern>.*/test/.*</exclude-pattern>

<rule ref="category/java/errorprone.xml">
<exclude name="DataflowAnomalyAnalysis"/>
<exclude name="MissingSerialVersionUID"/>
<exclude name="BeanMembersShouldSerialize"/>
</rule>

<rule ref="category/java/multithreading.xml">
<!-- Because it complains about the Map interface in the Resource class.-->
<!-- Because it complains for the Map interface in the Resource class.-->
<exclude name="UseConcurrentHashMap"/>
</rule>

Expand All @@ -40,8 +42,12 @@
<exclude name="UnnecessaryConstructor"/>
<!-- Conflicts with the rule category/java/codestyle.xml/AvoidProtectedMethodInFinalClassNotExtending -->
<exclude name="CommentDefaultAccessModifier"/>
<!-- We use vars! -->
<exclude name="UseExplicitTypes"/>
<!-- Conflicts with the rule category/java/codestyle.xml/AvoidProtectedMethodInFinalClassNotExtending -->
<exclude name="DefaultPackage"/>


<!--<exclude name="BeanMembersShouldSerialize"/>-->

<exclude name="ConfusingTernary"/>

<!-- printStackTrace is used in the Default Amazon handler in a static clause
Expand Down
30 changes: 13 additions & 17 deletions src/main/java/no/sikt/generator/ApiData.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.swagger.v3.oas.models.OpenAPI;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import nva.commons.core.JacocoGenerated;
import nva.commons.core.attempt.Failure;
Expand All @@ -23,10 +24,10 @@ public class ApiData {

private final RestApi awsRestApi;
private final OpenAPI openapiApiGateway;
private Optional<OpenAPI> openapiApiGithub;
private final String rawYaml;
private final Stage stage;
private static final Logger logger = LoggerFactory.getLogger(ApiData.class);
private OpenAPI openapiApiGithub;

public ApiData(RestApi awsRestApi, OpenAPI openapiApiGateway, String rawYaml, Stage stage) {
this.awsRestApi = awsRestApi;
Expand Down Expand Up @@ -63,10 +64,6 @@ public int getDashesInPath() {
return Try.attempt(this::getNumberOfDashesInBasePath).orElse(this::handleGetDashesFailure);
}

private Optional<OpenAPI> getOpenapiApiGithub() {
return Optional.ofNullable(openapiApiGithub);
}

public void setMatchingGithubOpenapi(List<Pair<S3Object, OpenAPI>> templateOpenapiDocs) {
var title = this.openapiApiGateway.getInfo().getTitle();
var matchingGithubOpenApi = templateOpenapiDocs.stream()
Expand All @@ -76,15 +73,16 @@ public void setMatchingGithubOpenapi(List<Pair<S3Object, OpenAPI>> templateOpena
.equals(title))
.findFirst();
if (matchingGithubOpenApi.isPresent()) {
logger.info("Using matching github openapi at "
+ matchingGithubOpenApi.get().getLeft().key()+ " for " + title);
this.openapiApiGithub = matchingGithubOpenApi.get().getRight();
logger.info("Using matching github openapi at " + matchingGithubOpenApi.get().getLeft().key()+ " for " + title);
this.openapiApiGithub = Optional.of(matchingGithubOpenApi.get().getRight());
} else {
logger.warn("No matching github openapi found for " + title);
this.openapiApiGithub = Optional.empty();
}
;
}

public ApiData applyEmptySchemasIfNull() {
public ApiData setEmptySchemasIfNull() {
if (isNull(this.openapiApiGateway.getComponents())) {
this.openapiApiGateway.setComponents(new Components());
}
Expand All @@ -95,29 +93,27 @@ public ApiData applyEmptySchemasIfNull() {
}

public ApiData overridePropsFromGithub() {
var openApiGithub = getOpenapiApiGithub();
torbjokv marked this conversation as resolved.
Show resolved Hide resolved
if (openApiGithub.isPresent()) {
if (this.openapiApiGithub.isPresent()) {
this.openapiApiGateway.getPaths().forEach((pathKey, pathItem) -> {
pathItem.readOperationsMap().forEach((httpMethod, operation) -> {
if (nonNull(operation.getParameters())) {
operation.getParameters().forEach(parameter -> {
var operationInGithub =
Optional.ofNullable(openApiGithub.get().getPaths().get(pathKey).readOperationsMap()
var operationInGithub = Optional.ofNullable(openapiApiGithub.get().getPaths().get(pathKey).readOperationsMap()
.get(httpMethod));
operationInGithub.ifPresent(value -> operation.setParameters(value.getParameters()));
});
}
});
});
if (nonNull(openApiGithub.get().getComponents())
&& nonNull(openApiGithub.get().getComponents().getSchemas())
if (nonNull(this.openapiApiGithub.get().getComponents())
&& nonNull(this.openapiApiGithub.get().getComponents().getSchemas())
) {
openApiGithub.get().getComponents().getSchemas().forEach((key,value) -> {
this.openapiApiGithub.get().getComponents().getSchemas().forEach(((key,value) -> {
if (isNull(this.openapiApiGateway.getComponents().getSchemas().get(key))) {
logger.info("Adding schema " + key + " from github");
this.openapiApiGateway.getComponents().getSchemas().put(key, value);
}
});
}));
}

}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/no/sikt/generator/ApplicationConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ private static String readExternalBucketName() {
private static String readInternalBucketName() {
return ENVIRONMENT.readEnv("INTERNAL_BUCKET_NAME");
}

public static String readOpenApiBucketName() {
return ENVIRONMENT.readEnv("OPEN_API_DOCS_BUCKET_NAME");
}
public static String readOpenApiBucketName() { return ENVIRONMENT.readEnv("OPEN_API_DOCS_BUCKET_NAME");}

private static String readExternalCloudFrontDistributionId() {
return ENVIRONMENT.readEnv("EXTERNAL_CLOUD_FRONT_DISTRIBUTION");
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/no/sikt/generator/OpenApiCombiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import java.util.Collection;
Expand Down Expand Up @@ -117,8 +118,8 @@ private void removeTags(OpenAPI api) {

private void mergeSecurity(OpenAPI api) {
if (nonNull(api.getSecurity())) {
for (var securityRequirement : api.getSecurity()) {
logger.info("adding security req {}", securityRequirement);
for (SecurityRequirement securityRequirement : api.getSecurity()) {
logger.info("adding security req {}", securityRequirement.toString());
this.baseTemplate.addSecurityItem(securityRequirement);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/no/sikt/generator/OpenApiExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.swagger.v3.oas.models.media.Schema;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.slf4j.Logger;
Expand All @@ -33,7 +34,7 @@ private void removeUnusedSchemas(OpenAPI openAPI) {
var usedSchemas = getUsedSchemas(openAPI);
var allSchemas = schemas.keySet();

var newSchemas = new HashMap<String, Schema>();
Map<String, Schema> newSchemas = new HashMap();

allSchemas.forEach(key -> {
var value = schemas.get(key);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/no/sikt/generator/OpenApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toSet;
import static java.util.stream.Stream.concat;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
Expand Down Expand Up @@ -95,12 +96,12 @@ public static List<Schema> getNestedSchemas(OpenAPI openAPI, Schema schema) {
public static List<Schema> getNestedSchemas(OpenAPI openAPI, List<Schema> visited, Schema schema) {
var nestedSchemas = Stream.of(
Stream.of(schema.getItems()),
getReffedSchema(openAPI, schema),
getNestedAllOfSchemas(schema),
getNestedAnyOfSchemas(schema),
getNestedOneOfSchemas(schema),
getNestedPropertiesSchemas(schema),
getAdditionalPropertiesSchemas(schema)
OpenApiUtils.getReffedSchema(openAPI, schema),
OpenApiUtils.getNestedAllOfSchemas(schema),
OpenApiUtils.getNestedAnyOfSchemas(schema),
OpenApiUtils.getNestedOneOfSchemas(schema),
OpenApiUtils.getNestedPropertiesSchemas(schema),
OpenApiUtils.getAdditionalPropertiesSchemas(schema)
).flatMap(stream -> stream).filter(Objects::nonNull).toList();

var newSchemas = nestedSchemas.stream().filter(ns -> !visited.contains(ns)).collect(toSet());
Expand All @@ -115,8 +116,7 @@ public static List<Schema> getNestedSchemas(OpenAPI openAPI, List<Schema> visite

private static Stream<Schema> getReffedSchema(OpenAPI openAPI, Schema schema) {
if (nonNull(schema.get$ref()) && schema.get$ref().startsWith(COMPONENTS_SCHEMAS)) {
return Stream.of(openAPI.getComponents()
.getSchemas().get(StringUtils.stripStart(schema.get$ref(), COMPONENTS_SCHEMAS)));
return Stream.of(openAPI.getComponents().getSchemas().get(StringUtils.stripStart(schema.get$ref(), COMPONENTS_SCHEMAS)));
} else {
return Stream.of();
}
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/no/sikt/generator/handlers/GenerateDocsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,15 @@ public GenerateDocsHandler() {
.retryPolicy(retryPolicy)
.build();

try (
var apiGatewayClient =
ApiGatewayAsyncClient.builder().overrideConfiguration(clientOverrideConfiguration).build()) {
this.apiGatewayHighLevelClient = new ApiGatewayHighLevelClient(apiGatewayClient);
}

try (var cloudFrontClient = CloudFrontClient.builder()
var apiGatewayClient =
ApiGatewayAsyncClient.builder().overrideConfiguration(clientOverrideConfiguration).build();
var cloudFrontClient = CloudFrontClient.builder()
.httpClient(UrlConnectionHttpClient.builder().build())
.region(AWS_GLOBAL)
.build()) {
this.cloudFrontHighLevelClient = new CloudFrontHighLevelClient(cloudFrontClient);
}
.build();

this.apiGatewayHighLevelClient = new ApiGatewayHighLevelClient(apiGatewayClient);
this.cloudFrontHighLevelClient = new CloudFrontHighLevelClient(cloudFrontClient);
this.s3ClientInput = S3Driver.defaultS3Client().build();
this.s3ClientOutput = S3Driver.defaultS3Client().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,14 @@ public void handleRequest(InputStream input, OutputStream output, Context contex
cloudFrontHighLevelClient.invalidateAll(EXTERNAL_CLOUD_FRONT_DISTRIBUTION);
}

private Stream<ApiData> validateAndFilterApis(GetRestApisResponse apis,
List<Pair<S3Object, OpenAPI>> templateOpenapiDocs) {
private Stream<ApiData> validateAndFilterApis(GetRestApisResponse apis, List<Pair<S3Object, OpenAPI>> templateOpenapiDocs) {
return apis.items().stream()
.map(this::fetchProdApiData)
.filter(Objects::nonNull)
.filter(this::apiShouldBeIncluded)
.peek(apiData -> openApiValidator.validateOpenApi(apiData.getOpenapi()))
.peek(apiData -> apiData.setMatchingGithubOpenapi(templateOpenapiDocs))
.map(ApiData::applyEmptySchemasIfNull)
.map(ApiData::setEmptySchemasIfNull)
.map(ApiData::overridePropsFromGithub)
.sorted(ApiData::sortByDate)
.sorted(ApiData::sortByDashes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ public void handleRequest(InputStream input, OutputStream output, Context contex
cloudFrontHighLevelClient.invalidateAll(INTERNAL_CLOUD_FRONT_DISTRIBUTION);
}

private Stream<ApiData> validateAndFilterApis(GetRestApisResponse apis,
List<Pair<S3Object, OpenAPI>> templateOpenapiDocs) {
private Stream<ApiData> validateAndFilterApis(GetRestApisResponse apis, List<Pair<S3Object, OpenAPI>> templateOpenapiDocs) {
return apis.items().stream()
.map(this::fetchProdApiData)
.filter(Objects::nonNull)
.filter(this::apiShouldBeIncluded)
.peek(apiData -> openApiValidator.validateOpenApi(apiData.getOpenapi()))
.peek(apiData -> apiData.setMatchingGithubOpenapi(templateOpenapiDocs))
.map(ApiData::applyEmptySchemasIfNull)
.map(ApiData::setEmptySchemasIfNull)
.map(ApiData::overridePropsFromGithub)
.sorted(ApiData::sortByDate)
.sorted(ApiData::sortByDashes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ public PublishDocumentationsHandler() {
.retryPolicy(retryPolicy)
.build();

try (var apiGatewayClient =
ApiGatewayAsyncClient.builder().overrideConfiguration(clientOverrideConfiguration).build()) {
var apiGatewayClient =
ApiGatewayAsyncClient.builder().overrideConfiguration(clientOverrideConfiguration).build();

this.apiGatewayHighLevelClient = new ApiGatewayHighLevelClient(apiGatewayClient);
}
this.apiGatewayHighLevelClient = new ApiGatewayHighLevelClient(apiGatewayClient);
}

public PublishDocumentationsHandler(ApiGatewayHighLevelClient apiGatewayHighLevelClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ public void shouldOverrideStyleIfSetInGithubOpenapi() {
handler.handleRequest(null, null, null);
var openApi = readGeneratedOpenApi();
var parameter =
openApi.getPaths().get("/publication/{publicationIdentifier}").getGet().getParameters().stream()
.filter(param -> param.getName().equals("doNotRedirect")).findFirst().get();
openApi.getPaths().get("/publication/{publicationIdentifier}").getGet().getParameters().stream().filter(param -> param.getName().equals("doNotRedirect")).findFirst().get();

assertThat(parameter.getStyle(), is(equalTo(StyleEnum.FORM)));
assertThat(parameter.getExplode(), is(equalTo(false)));
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/no/sikt/generator/handlers/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ private static RestApi buildRestApiFromTestCase(TestCase testCase) {
public static void loadGithubOpenapiFile(S3Driver s3Driver, TestCase testCase) {
if (testCase.getContentGithub().isPresent()) {
Try.attempt(() ->
s3Driver.insertFile(UnixPath.of(String.valueOf(Path.of(testCase.getId() + ".yaml"))),
testCase.getContentGithub().get())
s3Driver.insertFile(UnixPath.of(String.valueOf(Path.of(testCase.getId() + ".yaml")))
, testCase.getContentGithub().get())
);
}
}
Expand All @@ -87,8 +87,7 @@ public static void setupTestcasesFromFiles(S3Client s3Client,
List<Pair<String, Optional<String>>> fileNames) {
var filePrefix = "openapi_docs/" + ((isNull(folder)) ? "" : folder + "/");
var testCases = fileNames.stream().map(fileName -> loadTestCase(filePrefix + fileName.getLeft(),
fileName.getRight()
.map(fn -> filePrefix + fn))).toList();
fileName.getRight().map(fn -> filePrefix + fn)) ).toList();

var s3driver = new S3Driver(s3Client, readOpenApiBucketName());
testCases.forEach(tc -> loadGithubOpenapiFile(s3driver, tc));
Expand Down