Skip to content

Commit a5d15bf

Browse files
author
bnasslahsen
committed
project review
1 parent 3662e62 commit a5d15bf

25 files changed

+867
-238
lines changed

springdoc-openapi-data-rest/src/main/java/org/springdoc/core/HalProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import io.swagger.v3.core.converter.ModelConverters;
2424
import io.swagger.v3.core.util.Json;
25-
import org.springdoc.core.hal.CollectionModelContentConverter;
25+
import org.springdoc.core.converters.CollectionModelContentConverter;
2626

2727
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
2828
import org.springframework.hateoas.mediatype.hal.Jackson2HalModule;
@@ -40,7 +40,7 @@ private void init(){
4040
if(repositoryRestConfiguration.useHalAsDefaultJsonMediaType()) {
4141
Json.mapper().registerModule(new Jackson2HalModule());
4242
ModelConverters.getInstance()
43-
.addConverter(new CollectionModelContentConverter());
43+
.addConverter(CollectionModelContentConverter.getConverter());
4444
}
4545
}
4646
}

springdoc-openapi-data-rest/src/main/java/org/springdoc/core/SpringDocDataRestConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import io.swagger.v3.oas.models.media.StringSchema;
3030
import org.springdoc.core.converters.Pageable;
3131
import org.springdoc.core.customizers.OpenApiCustomiser;
32-
import org.springdoc.core.hal.RepresentationModelLinksOASMixin;
32+
import org.springdoc.core.converters.RepresentationModelLinksOASMixin;
3333

3434
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3535
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

springdoc-openapi-data-rest/src/main/java/org/springdoc/core/hal/CollectionModelContentConverter.java renamed to springdoc-openapi-data-rest/src/main/java/org/springdoc/core/converters/CollectionModelContentConverter.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
package org.springdoc.core.hal;
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package org.springdoc.core.converters;
20+
21+
import java.util.Collection;
22+
import java.util.Iterator;
223

324
import com.fasterxml.jackson.core.JsonGenerator;
425
import com.fasterxml.jackson.databind.SerializerProvider;
@@ -11,16 +32,22 @@
1132
import io.swagger.v3.oas.models.media.Schema;
1233
import io.swagger.v3.oas.models.media.StringSchema;
1334

14-
import java.util.Collection;
15-
import java.util.Iterator;
16-
1735
/**
1836
* Override resolved schema as there is a custom serializer that converts the data to a map before serializing it.
1937
*
2038
* @see org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalResourcesSerializer
2139
* @see org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalResourcesSerializer#serialize(Collection, JsonGenerator, SerializerProvider)
2240
*/
2341
public class CollectionModelContentConverter implements ModelConverter {
42+
43+
private static final CollectionModelContentConverter collectionModelContentConverter = new CollectionModelContentConverter();
44+
45+
private CollectionModelContentConverter() { }
46+
47+
public static CollectionModelContentConverter getConverter() {
48+
return collectionModelContentConverter;
49+
}
50+
2451
@Override
2552
public Schema<?> resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
2653
if (chain.hasNext() && type != null && type.getType() instanceof CollectionType
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package org.springdoc.core.converters;
20+
21+
import io.swagger.v3.oas.annotations.media.Schema;
22+
23+
import org.springframework.hateoas.Links;
24+
import org.springframework.hateoas.mediatype.hal.RepresentationModelMixin;
25+
26+
public abstract class RepresentationModelLinksOASMixin extends RepresentationModelMixin {
27+
@Override
28+
@Schema(ref = "#/components/schemas/Links")
29+
public abstract Links getLinks();
30+
}

springdoc-openapi-data-rest/src/main/java/org/springdoc/core/hal/RepresentationModelLinksOASMixin.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/AbstractSpringDocTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
import java.nio.file.Path;
2424
import java.nio.file.Paths;
2525

26+
import io.swagger.v3.core.converter.ModelConverters;
2627
import nonapi.io.github.classgraph.utils.FileUtils;
28+
import org.junit.jupiter.api.AfterAll;
2729
import org.junit.jupiter.api.Test;
2830
import org.slf4j.Logger;
2931
import org.slf4j.LoggerFactory;
3032
import org.springdoc.core.Constants;
33+
import org.springdoc.core.converters.CollectionModelContentConverter;
3134

3235
import org.springframework.beans.factory.annotation.Autowired;
3336
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@@ -77,4 +80,10 @@ public void testApp() throws Exception {
7780
String expected = new String(fileBytes);
7881
assertEquals(expected, result, true);
7982
}
83+
84+
@AfterAll
85+
public static void afterClass() {
86+
ModelConverters.getInstance().removeConverter(CollectionModelContentConverter.getConverter());
87+
System.clearProperty("spring.hateoas.use-hal-as-default-json-media-type");
88+
}
8089
}

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app2/DatabaseLoader.java renamed to springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app1/DatabaseLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* * limitations under the License.
1616
*
1717
*/
18-
package test.org.springdoc.api.app2;
18+
package test.org.springdoc.api.app1;
1919

2020
import org.springframework.boot.CommandLineRunner;
2121
import org.springframework.context.annotation.Bean;

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app2/Employee.java renamed to springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app1/Employee.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* * limitations under the License.
1616
*
1717
*/
18-
package test.org.springdoc.api.app2;
18+
package test.org.springdoc.api.app1;
1919

2020
import javax.persistence.Entity;
2121
import javax.persistence.GeneratedValue;

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app2/EmployeeController.java renamed to springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app1/EmployeeController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* * limitations under the License.
1616
*
1717
*/
18-
package test.org.springdoc.api.app2;
18+
package test.org.springdoc.api.app1;
1919

2020
import java.net.URI;
2121
import java.net.URISyntaxException;

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app2/EmployeeRepository.java renamed to springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app1/EmployeeRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* * limitations under the License.
1616
*
1717
*/
18-
package test.org.springdoc.api.app2;
18+
package test.org.springdoc.api.app1;
1919

2020
import org.springframework.data.repository.CrudRepository;
2121

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app1/SpringDocApp1Test.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020

2121
import test.org.springdoc.api.AbstractSpringDocTest;
2222

23-
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.test.context.TestPropertySource;
2424

25+
@TestPropertySource(properties = "spring.hateoas.use-hal-as-default-json-media-type= false")
2526
public class SpringDocApp1Test extends AbstractSpringDocTest {
2627

27-
@SpringBootApplication
28-
static class SpringDocTestApp {}
2928
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app1;
20+
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
25+
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
26+
import org.springframework.hateoas.server.core.EvoInflectorLinkRelationProvider;
27+
import org.springframework.http.MediaType;
28+
29+
@SpringBootApplication
30+
public class SpringDocTestApp {
31+
32+
public static void main(String[] args) {
33+
SpringApplication.run(SpringDocTestApp.class, args);
34+
}
35+
36+
@Bean
37+
public RepositoryRestConfigurer repositoryRestConfigurer() {
38+
return new RepositoryRestConfigurer() {
39+
@Override
40+
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
41+
config.setDefaultMediaType(MediaType.APPLICATION_JSON);
42+
config.useHalAsDefaultJsonMediaType(false);
43+
}
44+
};
45+
}
46+
47+
@Bean
48+
EvoInflectorLinkRelationProvider relProvider() {
49+
return new EvoInflectorLinkRelationProvider();
50+
}
51+
52+
}

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app1/HelloController.java renamed to springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app2/HelloController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
package test.org.springdoc.api.app1;
19+
package test.org.springdoc.api.app2;
2020

2121
import java.util.List;
2222

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app1/PersonDTO.java renamed to springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app2/PersonDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
package test.org.springdoc.api.app1;
19+
package test.org.springdoc.api.app2;
2020

2121
import io.swagger.v3.oas.annotations.media.Schema;
2222

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app2/SpringDocApp2Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020

2121
import test.org.springdoc.api.AbstractSpringDocTest;
2222

23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
2325
public class SpringDocApp2Test extends AbstractSpringDocTest {
2426

27+
@SpringBootApplication
28+
static class SpringDocTestApp {}
2529

2630
}

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app3/SpringDocApp3Test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ public class SpringDocApp3Test extends AbstractSpringDocTest {
2626

2727
@SpringBootApplication
2828
static class SpringDocTestApp {}
29+
2930
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
package test.org.springdoc.api.app4;
19+
20+
import org.springframework.boot.CommandLineRunner;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.stereotype.Component;
23+
24+
/**
25+
* Pre-load some data using a Spring Boot {@link CommandLineRunner}.
26+
*
27+
* @author Greg Turnquist
28+
*/
29+
@Component
30+
class DatabaseLoader {
31+
32+
/**
33+
* Use Spring to inject a {@link EmployeeRepository} that can then load data. Since this will run only after the app
34+
* is operational, the database will be up.
35+
*
36+
* @param repository
37+
*/
38+
@Bean
39+
CommandLineRunner init(EmployeeRepository repository) {
40+
41+
return args -> {
42+
repository.save(new Employee("Frodo", "Baggins", "ring bearer"));
43+
repository.save(new Employee("Bilbo", "Baggins", "burglar"));
44+
};
45+
}
46+
47+
}

0 commit comments

Comments
 (0)