mirror of
https://gitee.com/dcren/initializr.git
synced 2025-04-05 17:38:06 +08:00
Polish "Handle metadata resolution failures properly"
See gh-1039
This commit is contained in:
parent
81c66cd19e
commit
5d0c5333fe
@ -19,7 +19,6 @@ package io.spring.initializr.metadata;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
@ -186,27 +185,13 @@ public class BillOfMaterials {
|
|||||||
* additional BOMs to use, if any.
|
* additional BOMs to use, if any.
|
||||||
* @param bootVersion the Spring Boot version
|
* @param bootVersion the Spring Boot version
|
||||||
* @return the bill of materials
|
* @return the bill of materials
|
||||||
* @throws IllegalStateException if bom has mappings, none of which match the
|
* @throws InvalidInitializrMetadataException if no suitable mapping is found for that
|
||||||
* requested version
|
* version
|
||||||
*/
|
*/
|
||||||
public BillOfMaterials resolve(Version bootVersion) {
|
public BillOfMaterials resolve(Version bootVersion) {
|
||||||
return this.resolveSafe(bootVersion).orElseThrow(() -> new IllegalStateException(
|
|
||||||
"No suitable mapping was found for " + this + " and version " + bootVersion));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve this instance according to the specified Spring Boot {@link Version}.
|
|
||||||
* Return a {@link BillOfMaterials} instance that holds the version, repositories and
|
|
||||||
* additional BOMs to use, if any.
|
|
||||||
* @param bootVersion the Spring Boot version
|
|
||||||
* @return the bill of materials or empty if bom has mappings, none of which match the
|
|
||||||
* requested version
|
|
||||||
*/
|
|
||||||
public Optional<BillOfMaterials> resolveSafe(Version bootVersion) {
|
|
||||||
if (this.mappings.isEmpty()) {
|
if (this.mappings.isEmpty()) {
|
||||||
return Optional.of(this);
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Mapping mapping : this.mappings) {
|
for (Mapping mapping : this.mappings) {
|
||||||
if (mapping.range.match(bootVersion)) {
|
if (mapping.range.match(bootVersion)) {
|
||||||
BillOfMaterials resolvedBom = new BillOfMaterials(
|
BillOfMaterials resolvedBom = new BillOfMaterials(
|
||||||
@ -218,10 +203,11 @@ public class BillOfMaterials {
|
|||||||
.addAll(!mapping.repositories.isEmpty() ? mapping.repositories : this.repositories);
|
.addAll(!mapping.repositories.isEmpty() ? mapping.repositories : this.repositories);
|
||||||
resolvedBom.additionalBoms
|
resolvedBom.additionalBoms
|
||||||
.addAll(!mapping.additionalBoms.isEmpty() ? mapping.additionalBoms : this.additionalBoms);
|
.addAll(!mapping.additionalBoms.isEmpty() ? mapping.additionalBoms : this.additionalBoms);
|
||||||
return Optional.of(resolvedBom);
|
return resolvedBom;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
throw new InvalidInitializrMetadataException(
|
||||||
|
"No suitable mapping was found for " + this + " and version " + bootVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -24,7 +24,7 @@ import io.spring.initializr.metadata.BillOfMaterials.Mapping;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
@ -119,17 +119,8 @@ class BillOfMaterialsTests {
|
|||||||
bom.getMappings().add(Mapping.create("[1.2.0.RELEASE,1.3.0.M1)", "1.1.0"));
|
bom.getMappings().add(Mapping.create("[1.2.0.RELEASE,1.3.0.M1)", "1.1.0"));
|
||||||
bom.getMappings().add(Mapping.create("[1.3.0.M1, 1.4.0.M1)", "1.2.0"));
|
bom.getMappings().add(Mapping.create("[1.3.0.M1, 1.4.0.M1)", "1.2.0"));
|
||||||
bom.validate();
|
bom.validate();
|
||||||
assertThatIllegalStateException().isThrownBy(() -> bom.resolve(Version.parse("1.4.1.RELEASE")))
|
assertThatExceptionOfType(InvalidInitializrMetadataException.class)
|
||||||
.withMessageContaining("1.4.1.RELEASE");
|
.isThrownBy(() -> bom.resolve(Version.parse("1.4.1.RELEASE"))).withMessageContaining("1.4.1.RELEASE");
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void noRangeAvailableSafe() {
|
|
||||||
BillOfMaterials bom = BillOfMaterials.create("com.example", "bom");
|
|
||||||
bom.getMappings().add(Mapping.create("[1.2.0.RELEASE,1.3.0.M1)", "1.1.0"));
|
|
||||||
bom.getMappings().add(Mapping.create("[1.3.0.M1, 1.4.0.M1)", "1.2.0"));
|
|
||||||
bom.validate();
|
|
||||||
assertThat(bom.resolveSafe(Version.parse("1.4.1.RELEASE"))).isEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1,31 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -26,6 +26,7 @@ import io.spring.initializr.metadata.DependencyMetadata;
|
|||||||
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
||||||
import io.spring.initializr.metadata.InitializrMetadata;
|
import io.spring.initializr.metadata.InitializrMetadata;
|
||||||
import io.spring.initializr.metadata.InitializrMetadataProvider;
|
import io.spring.initializr.metadata.InitializrMetadataProvider;
|
||||||
|
import io.spring.initializr.metadata.InvalidInitializrMetadataException;
|
||||||
import io.spring.initializr.web.mapper.DependencyMetadataV21JsonMapper;
|
import io.spring.initializr.web.mapper.DependencyMetadataV21JsonMapper;
|
||||||
import io.spring.initializr.web.mapper.InitializrMetadataJsonMapper;
|
import io.spring.initializr.web.mapper.InitializrMetadataJsonMapper;
|
||||||
import io.spring.initializr.web.mapper.InitializrMetadataV21JsonMapper;
|
import io.spring.initializr.web.mapper.InitializrMetadataV21JsonMapper;
|
||||||
@ -91,7 +92,7 @@ public class ProjectMetadataController extends AbstractMetadataController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler
|
@ExceptionHandler
|
||||||
public void invalidMetadaRequest(HttpServletResponse response, InvalidMetadataRequestException ex)
|
public void invalidMetadataRequest(HttpServletResponse response, InvalidInitializrMetadataException ex)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
response.sendError(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
|
response.sendError(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,6 @@ import io.spring.initializr.metadata.DependencyMetadata;
|
|||||||
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
||||||
import io.spring.initializr.metadata.InitializrMetadata;
|
import io.spring.initializr.metadata.InitializrMetadata;
|
||||||
import io.spring.initializr.metadata.Repository;
|
import io.spring.initializr.metadata.Repository;
|
||||||
import io.spring.initializr.web.controller.InvalidMetadataRequestException;
|
|
||||||
|
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
|
||||||
@ -58,12 +57,8 @@ public class DefaultDependencyMetadataProvider implements DependencyMetadataProv
|
|||||||
Map<String, BillOfMaterials> boms = new LinkedHashMap<>();
|
Map<String, BillOfMaterials> boms = new LinkedHashMap<>();
|
||||||
for (Dependency dependency : dependencies.values()) {
|
for (Dependency dependency : dependencies.values()) {
|
||||||
if (dependency.getBom() != null) {
|
if (dependency.getBom() != null) {
|
||||||
BillOfMaterials bom = metadata.getConfiguration().getEnv().getBoms().get(dependency.getBom())
|
boms.put(dependency.getBom(),
|
||||||
.resolveSafe(bootVersion)
|
metadata.getConfiguration().getEnv().getBoms().get(dependency.getBom()).resolve(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
|
// Each resolved bom may require additional repositories
|
||||||
|
@ -61,7 +61,7 @@ public class ProjectMetadataControllerIntegrationTests extends AbstractInitializ
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void metadataWithInvalidBootVersion() {
|
void metadataWithInvalidPlatformVersion() {
|
||||||
try {
|
try {
|
||||||
execute("/dependencies?bootVersion=1.5.17.RELEASE", String.class, "application/vnd.initializr.v2.1+json",
|
execute("/dependencies?bootVersion=1.5.17.RELEASE", String.class, "application/vnd.initializr.v2.1+json",
|
||||||
"application/json");
|
"application/json");
|
||||||
|
@ -23,11 +23,9 @@ import io.spring.initializr.metadata.Dependency;
|
|||||||
import io.spring.initializr.metadata.DependencyMetadata;
|
import io.spring.initializr.metadata.DependencyMetadata;
|
||||||
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
||||||
import io.spring.initializr.metadata.InitializrMetadata;
|
import io.spring.initializr.metadata.InitializrMetadata;
|
||||||
import io.spring.initializr.web.controller.InvalidMetadataRequestException;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
@ -76,21 +74,6 @@ class DefaultDependencyMetadataProviderTests {
|
|||||||
assertThat(anotherDependencyMetadata.getDependencies().get("first").getVersion()).isEqualTo("0.2.0.RELEASE");
|
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
|
@Test
|
||||||
void addRepoAndRemoveDuplicates() {
|
void addRepoAndRemoveDuplicates() {
|
||||||
Dependency first = Dependency.withId("first", "org.foo", "first");
|
Dependency first = Dependency.withId("first", "org.foo", "first");
|
||||||
|
Loading…
Reference in New Issue
Block a user