Skip to content

Commit 765607d

Browse files
committed
Only exported association properties should be ignored in schema
1 parent a714f41 commit 765607d

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/SpringDocDataRestUtils.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.springframework.data.mapping.PersistentProperty;
5656
import org.springframework.data.mapping.SimpleAssociationHandler;
5757
import org.springframework.data.mapping.context.PersistentEntities;
58+
import org.springframework.data.rest.core.annotation.RestResource;
5859
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
5960
import org.springframework.data.rest.core.mapping.ResourceMappings;
6061
import org.springframework.data.rest.core.mapping.ResourceMetadata;
@@ -133,7 +134,7 @@ public void customise(OpenAPI openAPI, ResourceMappings mappings, PersistentEnti
133134
entityInfo.setAssociationsFields(associationsFields);
134135
entityInoMap.put(domainType.getSimpleName(), entityInfo);
135136
}
136-
137+
137138
openAPI.getPaths().entrySet().stream()
138139
.forEach(stringPathItemEntry -> {
139140
PathItem pathItem = stringPathItemEntry.getValue();
@@ -417,9 +418,11 @@ private List<String> getAssociationsFields(ResourceMetadata
417418
List<String> associationsFields = new ArrayList<>();
418419
entity.doWithAssociations((SimpleAssociationHandler) association -> {
419420
PersistentProperty<?> property = association.getInverse();
420-
String filedName = resourceMetadata.getMappingFor(property).getRel().value();
421-
associationsFields.add(filedName);
422-
});
421+
if (isAssociationExported(property)) {
422+
String filedName = resourceMetadata.getMappingFor(property).getRel().value();
423+
associationsFields.add(filedName);
424+
}
425+
});
423426
return associationsFields;
424427
}
425428

@@ -438,13 +441,31 @@ private List<String> getIgnoredFields(ResourceMetadata
438441
ignoredFields.add(idField);
439442
entity.doWithAssociations((SimpleAssociationHandler) association -> {
440443
PersistentProperty<?> property = association.getInverse();
441-
String filedName = resourceMetadata.getMappingFor(property).getRel().value();
442-
ignoredFields.add(filedName);
443-
});
444+
if (isAssociationExported(property)) {
445+
String filedName = resourceMetadata.getMappingFor(property).getRel().value();
446+
ignoredFields.add(filedName);
447+
}
448+
});
444449
}
445450
return ignoredFields;
446451
}
447452

453+
/**
454+
* Indicates if an association is exported. If not, it should be included in response schema.
455+
*
456+
* @param associationProperty the property to check
457+
* @return true if a field is not exported
458+
*/
459+
private boolean isAssociationExported(PersistentProperty<?> associationProperty) {
460+
try {
461+
RestResource restResource = associationProperty.getRequiredAnnotation(RestResource.class);
462+
return restResource.exported();
463+
} catch (IllegalStateException e) {
464+
//if annotation missing, association is exported by default
465+
return true;
466+
}
467+
}
468+
448469
/**
449470
* Build text uri content.
450471
*

0 commit comments

Comments
 (0)