Fix Sagan metadata reader to not rely on JSON order

This commit makes sure that Spring Boot versions are sorted from the
newest to the oldest, irrespective of the order of the metadata
elements in the json response.

Closes gh-1128
This commit is contained in:
Stephane Nicoll
2020-09-26 07:53:16 +02:00
parent 35ef5df63e
commit 326d0813a3
2 changed files with 32 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -18,6 +18,7 @@ package io.spring.initializr.web.support;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
@@ -38,6 +39,8 @@ import org.springframework.web.client.RestTemplate;
*/
class SpringBootMetadataReader {
private static final Comparator<DefaultMetadataElement> VERSION_METADATA_ELEMENT_COMPARATOR = new VersionMetadataElementComparator();
private final JsonNode content;
/**
@@ -64,6 +67,7 @@ class SpringBootMetadataReader {
list.add(versionMetadata);
}
}
list.sort(VERSION_METADATA_ELEMENT_COMPARATOR.reversed());
return list;
}
@@ -108,4 +112,17 @@ class SpringBootMetadataReader {
return sb.toString();
}
private static class VersionMetadataElementComparator implements Comparator<DefaultMetadataElement> {
private static final VersionParser versionParser = VersionParser.DEFAULT;
@Override
public int compare(DefaultMetadataElement o1, DefaultMetadataElement o2) {
Version o1Version = versionParser.parse(o1.getId());
Version o2Version = versionParser.parse(o2.getId());
return o1Version.compareTo(o2Version);
}
}
}

View File

@@ -6,6 +6,20 @@
"category": "active",
"stackOverflowTags": "spring-boot",
"projectReleases": [
{
"releaseStatus": "GENERAL_AVAILABILITY",
"refDocUrl": "https://docs.spring.io/spring-boot/docs/2.4.0/reference/htmlsingle/",
"apiDocUrl": "https://docs.spring.io/spring-boot/docs/2.4.0/api/",
"groupId": "org.springframework.boot",
"artifactId": "spring-boot",
"repository": null,
"preRelease": false,
"generalAvailability": true,
"versionDisplayName": "2.4.0",
"current": true,
"snapshot": false,
"version": "2.4.0"
},
{
"releaseStatus": "SNAPSHOT",
"refDocUrl": "https://docs.spring.io/spring-boot/docs/2.5.0-M1/reference/htmlsingle/",
@@ -44,20 +58,6 @@
"snapshot": true,
"version": "2.4.1-SNAPSHOT"
},
{
"releaseStatus": "GENERAL_AVAILABILITY",
"refDocUrl": "https://docs.spring.io/spring-boot/docs/2.4.0/reference/htmlsingle/",
"apiDocUrl": "https://docs.spring.io/spring-boot/docs/2.4.0/api/",
"groupId": "org.springframework.boot",
"artifactId": "spring-boot",
"repository": null,
"preRelease": false,
"generalAvailability": true,
"versionDisplayName": "2.4.0",
"current": true,
"snapshot": false,
"version": "2.4.0"
},
{
"releaseStatus": "SNAPSHOT",
"refDocUrl": "https://docs.spring.io/spring-boot/docs/2.3.8.BUILD-SNAPSHOT/reference/htmlsingle/",