mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-19 10:08:22 +08:00
Handle metadata resolution failures properly
See gh-1039
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.initializr.web.controller;
|
||||
|
||||
/**
|
||||
* Thrown when a metadata request is invalid.
|
||||
*
|
||||
* @author Chris Bono
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class InvalidMetadataRequestException extends RuntimeException {
|
||||
|
||||
public InvalidMetadataRequestException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@@ -16,8 +16,11 @@
|
||||
|
||||
package io.spring.initializr.web.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import io.spring.initializr.generator.version.Version;
|
||||
import io.spring.initializr.metadata.DependencyMetadata;
|
||||
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
||||
@@ -30,9 +33,11 @@ import io.spring.initializr.web.mapper.InitializrMetadataV2JsonMapper;
|
||||
import io.spring.initializr.web.mapper.InitializrMetadataVersion;
|
||||
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@@ -85,6 +90,12 @@ public class ProjectMetadataController extends AbstractMetadataController {
|
||||
return dependenciesFor(InitializrMetadataVersion.V2_1, bootVersion);
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
public void invalidMetadaRequest(HttpServletResponse response, InvalidMetadataRequestException ex)
|
||||
throws IOException {
|
||||
response.sendError(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link CacheControl} response headers to use for the specified
|
||||
* {@link InitializrMetadata metadata}. If no cache should be applied
|
||||
|
@@ -26,6 +26,7 @@ import io.spring.initializr.metadata.DependencyMetadata;
|
||||
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
||||
import io.spring.initializr.metadata.InitializrMetadata;
|
||||
import io.spring.initializr.metadata.Repository;
|
||||
import io.spring.initializr.web.controller.InvalidMetadataRequestException;
|
||||
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
|
||||
@@ -57,8 +58,12 @@ public class DefaultDependencyMetadataProvider implements DependencyMetadataProv
|
||||
Map<String, BillOfMaterials> boms = new LinkedHashMap<>();
|
||||
for (Dependency dependency : dependencies.values()) {
|
||||
if (dependency.getBom() != null) {
|
||||
boms.put(dependency.getBom(),
|
||||
metadata.getConfiguration().getEnv().getBoms().get(dependency.getBom()).resolve(bootVersion));
|
||||
BillOfMaterials bom = metadata.getConfiguration().getEnv().getBoms().get(dependency.getBom())
|
||||
.resolveSafe(bootVersion)
|
||||
.orElseThrow(() -> new InvalidMetadataRequestException(String.format(
|
||||
"Dependency '%s' points to Bom '%s' that is not compatible with requested platform version '%s'.",
|
||||
dependency.getId(), dependency.getBom(), bootVersion)));
|
||||
boms.put(dependency.getBom(), bom);
|
||||
}
|
||||
}
|
||||
// Each resolved bom may require additional repositories
|
||||
|
@@ -60,6 +60,18 @@ public class ProjectMetadataControllerIntegrationTests extends AbstractInitializ
|
||||
validateMetadata(response, InitializrMetadataVersion.V2.getMediaType(), "2.0.0", JSONCompareMode.STRICT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void metadataWithInvalidBootVersion() {
|
||||
try {
|
||||
execute("/dependencies?bootVersion=1.5.17.RELEASE", String.class, "application/vnd.initializr.v2.1+json",
|
||||
"application/json");
|
||||
}
|
||||
catch (HttpClientErrorException ex) {
|
||||
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat(ex.getResponseBodyAsString().contains("1.5.17.RELEASE"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void metadataWithCurrentAcceptHeader() {
|
||||
getRequests().setFields("_links.maven-project", "dependencies.values[0]", "type.values[0]",
|
||||
|
@@ -23,9 +23,11 @@ import io.spring.initializr.metadata.Dependency;
|
||||
import io.spring.initializr.metadata.DependencyMetadata;
|
||||
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
||||
import io.spring.initializr.metadata.InitializrMetadata;
|
||||
import io.spring.initializr.web.controller.InvalidMetadataRequestException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* @author Stephane Nicoll
|
||||
@@ -74,6 +76,21 @@ class DefaultDependencyMetadataProviderTests {
|
||||
assertThat(anotherDependencyMetadata.getDependencies().get("first").getVersion()).isEqualTo("0.2.0.RELEASE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveBomInvalidBootVersion() {
|
||||
Dependency first = Dependency.withId("first", "org.foo", "first");
|
||||
first.setRepository("repo-foo");
|
||||
first.setBom("bom-foo");
|
||||
BillOfMaterials bom = BillOfMaterials.create("org.foo", "bom");
|
||||
bom.getMappings()
|
||||
.add(BillOfMaterials.Mapping.create("[1.0.0.RELEASE, 1.1.0.RELEASE)", "2.0.0.RELEASE", "repo-foo"));
|
||||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults().addBom("bom-foo", bom)
|
||||
.addRepository("repo-foo", "foo", "http://localhost", false).addDependencyGroup("test", first).build();
|
||||
assertThatExceptionOfType(InvalidMetadataRequestException.class)
|
||||
.isThrownBy(() -> this.provider.get(metadata, Version.parse("1.1.5.RELEASE")))
|
||||
.withMessageContainingAll(first.getId(), first.getBom(), "1.1.5.RELEASE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addRepoAndRemoveDuplicates() {
|
||||
Dependency first = Dependency.withId("first", "org.foo", "first");
|
||||
|
Reference in New Issue
Block a user