Skip to content

Commit c065481

Browse files
committed
Expose @id fields for entity with spring-data-rest/springdoc-openapi-data-rest. Fixes #1146
1 parent 182d9e1 commit c065481

File tree

8 files changed

+720
-5
lines changed

8 files changed

+720
-5
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,13 @@ DataRestTagsService dataRestTagsBuilder(OpenAPIService openAPIService) {
259259
* Spring doc data rest utils spring doc data rest utils.
260260
*
261261
* @param linkRelationProvider the link relation provider
262+
* @param repositoryRestConfiguration the repository rest configuration
262263
* @return the spring doc data rest utils
263264
*/
264265
@Bean
265266
@ConditionalOnMissingBean
266-
SpringDocDataRestUtils springDocDataRestUtils(LinkRelationProvider linkRelationProvider) {
267-
return new SpringDocDataRestUtils(linkRelationProvider);
267+
SpringDocDataRestUtils springDocDataRestUtils(LinkRelationProvider linkRelationProvider, RepositoryRestConfiguration repositoryRestConfiguration) {
268+
return new SpringDocDataRestUtils(linkRelationProvider, repositoryRestConfiguration);
268269
}
269270
}
270271

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/utils/EntityInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.springdoc.data.rest.utils;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
/**
@@ -11,7 +12,7 @@ public class EntityInfo {
1112
/**
1213
* The Ignored fields.
1314
*/
14-
private List<String> ignoredFields;
15+
private List<String> ignoredFields = new ArrayList<>();
1516

1617
/**
1718
* The Associations fields.

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/utils/SpringDocDataRestUtils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.data.mapping.PersistentProperty;
3131
import org.springframework.data.mapping.SimpleAssociationHandler;
3232
import org.springframework.data.mapping.context.PersistentEntities;
33+
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
3334
import org.springframework.data.rest.core.mapping.ResourceMappings;
3435
import org.springframework.data.rest.core.mapping.ResourceMetadata;
3536
import org.springframework.data.rest.webmvc.RestMediaTypes;
@@ -67,13 +68,20 @@ public class SpringDocDataRestUtils {
6768
*/
6869
private HashMap<String, EntityInfo> entityInoMap = new HashMap();
6970

71+
/**
72+
* The Repository rest configuration.
73+
*/
74+
private RepositoryRestConfiguration repositoryRestConfiguration;
75+
7076
/**
7177
* Instantiates a new Spring doc data rest utils.
7278
*
7379
* @param linkRelationProvider the link relation provider
80+
* @param repositoryRestConfiguration the repository rest configuration
7481
*/
75-
public SpringDocDataRestUtils(LinkRelationProvider linkRelationProvider) {
82+
public SpringDocDataRestUtils(LinkRelationProvider linkRelationProvider, RepositoryRestConfiguration repositoryRestConfiguration) {
7683
this.linkRelationProvider = linkRelationProvider;
84+
this.repositoryRestConfiguration = repositoryRestConfiguration;
7785
}
7886

7987
/**
@@ -93,7 +101,8 @@ public void customise(OpenAPI openAPI, ResourceMappings mappings, PersistentEnti
93101
EntityInfo entityInfo = new EntityInfo();
94102
entityInfo.setDomainType(domainType);
95103
List<String> ignoredFields = getIgnoredFields(resourceMetadata, entity);
96-
entityInfo.setIgnoredFields(ignoredFields);
104+
if (!repositoryRestConfiguration.isIdExposedFor(entity.getType()))
105+
entityInfo.setIgnoredFields(ignoredFields);
97106
List<String> associationsFields = getAssociationsFields(resourceMetadata, entity);
98107
entityInfo.setAssociationsFields(associationsFields);
99108
entityInoMap.put(domainType.getSimpleName(), entityInfo);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.api.app26;
25+
26+
import javax.persistence.Entity;
27+
import javax.persistence.GeneratedValue;
28+
import javax.persistence.GenerationType;
29+
import javax.persistence.Id;
30+
31+
@Entity
32+
public class Person {
33+
34+
@Id
35+
@GeneratedValue(strategy = GenerationType.AUTO)
36+
private long id;
37+
38+
private String firstName;
39+
private String lastName;
40+
41+
public long getId() {
42+
return id;
43+
}
44+
45+
public void setId(long id) {
46+
this.id = id;
47+
}
48+
49+
public String getFirstName() {
50+
return firstName;
51+
}
52+
53+
public void setFirstName(String firstName) {
54+
this.firstName = firstName;
55+
}
56+
57+
public String getLastName() {
58+
return lastName;
59+
}
60+
61+
public void setLastName(String lastName) {
62+
this.lastName = lastName;
63+
}
64+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.api.app26;
25+
26+
import java.util.List;
27+
28+
import org.springframework.data.repository.PagingAndSortingRepository;
29+
import org.springframework.data.repository.query.Param;
30+
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
31+
32+
@RepositoryRestResource(collectionResourceRel = "people", path = "peopleme")
33+
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
34+
35+
List<Person> findByLastName(@Param("name") String name);
36+
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.api.app26;
25+
26+
import test.org.springdoc.api.AbstractSpringDocTest;
27+
28+
import org.springframework.boot.autoconfigure.SpringBootApplication;
29+
30+
public class SpringDocApp26Test extends AbstractSpringDocTest {
31+
32+
@SpringBootApplication
33+
static class SpringDocTestApp {
34+
}
35+
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test.org.springdoc.api.app26;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
5+
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
6+
7+
@Configuration
8+
public class SpringRestConfiguration implements RepositoryRestConfigurer {
9+
@Override
10+
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
11+
config.exposeIdsFor(Person.class);
12+
}
13+
}

0 commit comments

Comments
 (0)