mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-19 01:58:16 +08:00
Enable strict validation of dependencies
Previously, if one invokes the service asking for dependency `foo` and `foo` does not exist we created a starter for it, that is `spring-boot-starter-foo`. This mechanism was put in place because we don't know all starters and to offer a nice fallback for users who know what they're doing. That statement proved to be wrong since users actually discover new starters via the service and don't seem to attempt to create such starter that are located in the `org.springframework.boot` groupId anyway. Most if not all of those are pilot errors. This commit enables strict validation of dependencies and generate an appropriate exception if it isn't defined in the meta-data. Closes gh-234
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package io.spring.initializr.web.project
|
||||
|
||||
import javax.annotation.PostConstruct
|
||||
import javax.servlet.http.HttpServletResponse
|
||||
|
||||
import io.spring.initializr.generator.InvalidProjectRequestException
|
||||
import io.spring.initializr.metadata.InitializrMetadataProvider
|
||||
@@ -24,7 +25,6 @@ import io.spring.initializr.metadata.InitializrMetadataProvider
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||
import org.springframework.web.bind.annotation.ResponseStatus
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder
|
||||
|
||||
import static io.spring.initializr.util.GroovyTemplate.template
|
||||
@@ -47,10 +47,9 @@ abstract class AbstractInitializrController {
|
||||
forceSsl = metadataProvider.get().configuration.env.forceSsl
|
||||
}
|
||||
|
||||
@ExceptionHandler(InvalidProjectRequestException)
|
||||
@ResponseStatus(value= HttpStatus.BAD_REQUEST)
|
||||
public void invalidProjectVersion() {
|
||||
// Nothing to do
|
||||
@ExceptionHandler
|
||||
public void invalidProjectRequest(HttpServletResponse response, InvalidProjectRequestException ex) {
|
||||
response.sendError(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -16,10 +16,10 @@
|
||||
|
||||
package io.spring.initializr.web.project
|
||||
|
||||
import groovy.json.JsonSlurper
|
||||
import io.spring.initializr.metadata.Dependency
|
||||
import io.spring.initializr.web.AbstractInitializrControllerIntegrationTests
|
||||
import io.spring.initializr.web.mapper.InitializrMetadataVersion
|
||||
import io.spring.initializr.web.project.MainController
|
||||
import org.json.JSONObject
|
||||
import org.junit.Ignore
|
||||
import org.junit.Test
|
||||
@@ -41,6 +41,7 @@ import static org.junit.Assert.assertFalse
|
||||
import static org.junit.Assert.assertNotNull
|
||||
import static org.junit.Assert.assertThat
|
||||
import static org.junit.Assert.assertTrue
|
||||
import static org.junit.Assert.fail
|
||||
|
||||
/**
|
||||
* @author Stephane Nicoll
|
||||
@@ -326,15 +327,24 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
|
||||
void missingDependencyProperException() {
|
||||
try {
|
||||
downloadArchive('/starter.zip?style=foo:bar')
|
||||
fail("Should have failed")
|
||||
} catch (HttpClientErrorException ex) {
|
||||
assertEquals HttpStatus.BAD_REQUEST, ex.getStatusCode()
|
||||
assertStandardErrorBody(ex.getResponseBodyAsString(),
|
||||
"Unknown dependency 'foo:bar' check project metadata")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void downloadWithUnknownSpringBootStarter() { // Simple id are accepted as spring-boot-starter
|
||||
downloadZip('/starter.zip?style=foo').pomAssert().hasSpringBootStarterDependency('foo')
|
||||
void invalidDependencyProperException() {
|
||||
try {
|
||||
downloadArchive('/starter.zip?style=foo')
|
||||
fail("Should have failed")
|
||||
} catch (HttpClientErrorException ex) {
|
||||
assertEquals HttpStatus.BAD_REQUEST, ex.getStatusCode()
|
||||
assertStandardErrorBody(ex.getResponseBodyAsString(),
|
||||
"Unknown dependency 'foo' check project metadata")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -412,5 +422,10 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
|
||||
return new JSONObject(json)
|
||||
}
|
||||
|
||||
private static void assertStandardErrorBody(String body, String message) {
|
||||
assertNotNull "error body must be available", body
|
||||
def model = new JsonSlurper().parseText(body)
|
||||
assertEquals message, model.message
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user