Skip to content

Commit 3d93133

Browse files
committed
Add InitializrMetadataV23JsonMapper
Signed-off-by: sijun-yang <[email protected]>
1 parent 661049d commit 3d93133

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012 - present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.web.mapper;
18+
19+
import com.fasterxml.jackson.databind.node.ObjectNode;
20+
import io.spring.initializr.metadata.InitializrMetadata;
21+
import io.spring.initializr.metadata.Type;
22+
23+
import org.springframework.hateoas.TemplateVariable;
24+
import org.springframework.hateoas.TemplateVariables;
25+
26+
/**
27+
* A {@link InitializrMetadataJsonMapper} handling the metadata format for v2.3.
28+
* <p>
29+
* Version 2.3 adds support for configuration file formats, allowing users to specify
30+
* their preferred configuration file format (e.g., properties, YAML).
31+
*
32+
* @author Sijun Yang
33+
* @see InitializrMetadataVersion#V2_3
34+
*/
35+
public class InitializrMetadataV23JsonMapper extends InitializrMetadataV22JsonMapper {
36+
37+
@Override
38+
protected TemplateVariables getTemplateVariables(Type type) {
39+
return super.getTemplateVariables(type)
40+
.concat(new TemplateVariable("configurationFileFormat", TemplateVariable.VariableType.REQUEST_PARAM));
41+
}
42+
43+
@Override
44+
protected void customizeParent(ObjectNode parent, InitializrMetadata metadata) {
45+
super.customizeParent(parent, metadata);
46+
singleSelect(parent, metadata.getConfigurationFileFormats());
47+
}
48+
49+
}

initializr-web/src/main/java/io/spring/initializr/web/mapper/InitializrMetadataVersion.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public enum InitializrMetadataVersion {
4040
/**
4141
* Add support for SemVer compliant version format.
4242
*/
43-
V2_2("application/vnd.initializr.v2.2+json");
43+
V2_2("application/vnd.initializr.v2.2+json"),
44+
45+
/**
46+
* Add support for selecting the project's configuration file format.
47+
*/
48+
V2_3("application/vnd.initializr.v2.3+json");
4449

4550
private final MediaType mediaType;
4651

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012 - present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.web.mapper;
18+
19+
import com.fasterxml.jackson.core.JsonProcessingException;
20+
import com.fasterxml.jackson.databind.JsonNode;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import io.spring.initializr.generator.test.InitializrMetadataTestBuilder;
23+
import io.spring.initializr.metadata.InitializrMetadata;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link InitializrMetadataV23JsonMapper}.
30+
*
31+
* @author Sijun Yang
32+
*/
33+
class InitializrMetadataV23JsonMapperTests {
34+
35+
private static final ObjectMapper objectMapper = new ObjectMapper();
36+
37+
private final InitializrMetadataV23JsonMapper mapper = new InitializrMetadataV23JsonMapper();
38+
39+
@Test
40+
void writeConfigurationFileFormatMetadata() throws JsonProcessingException {
41+
InitializrMetadata metadata = new InitializrMetadataTestBuilder()
42+
.addConfigurationFileFormats("properties", true)
43+
.addConfigurationFileFormats("yaml", false)
44+
.build();
45+
String json = this.mapper.write(metadata, null);
46+
JsonNode result = objectMapper.readTree(json);
47+
48+
assertThat(result.has("configurationFileFormat")).isTrue();
49+
JsonNode formatsNode = result.get("configurationFileFormat");
50+
assertThat(formatsNode.get("type").asText()).isEqualTo("single-select");
51+
assertThat(formatsNode.get("default").asText()).isEqualTo("properties");
52+
assertThat(formatsNode.get("values")).hasSize(2);
53+
}
54+
55+
@Test
56+
void writeTemplatedUriWithConfigurationFileFormatParameter() throws JsonProcessingException {
57+
InitializrMetadata metadata = new InitializrMetadataTestBuilder()
58+
.addType("id", true, "action", "build", "dialect", "format")
59+
.build();
60+
String json = this.mapper.write(metadata, null);
61+
JsonNode result = objectMapper.readTree(json);
62+
63+
assertThat(result.at("/_links/id/href").asText())
64+
.isEqualTo("/action?type=id{&dependencies,packaging,javaVersion,"
65+
+ "language,bootVersion,groupId,artifactId,version,name,description,packageName,configurationFileFormat}");
66+
}
67+
68+
}

0 commit comments

Comments
 (0)