55
55
import org .springframework .data .mapping .PersistentProperty ;
56
56
import org .springframework .data .mapping .SimpleAssociationHandler ;
57
57
import org .springframework .data .mapping .context .PersistentEntities ;
58
+ import org .springframework .data .rest .core .annotation .RestResource ;
58
59
import org .springframework .data .rest .core .config .RepositoryRestConfiguration ;
59
60
import org .springframework .data .rest .core .mapping .ResourceMappings ;
60
61
import org .springframework .data .rest .core .mapping .ResourceMetadata ;
@@ -133,7 +134,7 @@ public void customise(OpenAPI openAPI, ResourceMappings mappings, PersistentEnti
133
134
entityInfo .setAssociationsFields (associationsFields );
134
135
entityInoMap .put (domainType .getSimpleName (), entityInfo );
135
136
}
136
-
137
+
137
138
openAPI .getPaths ().entrySet ().stream ()
138
139
.forEach (stringPathItemEntry -> {
139
140
PathItem pathItem = stringPathItemEntry .getValue ();
@@ -417,9 +418,11 @@ private List<String> getAssociationsFields(ResourceMetadata
417
418
List <String > associationsFields = new ArrayList <>();
418
419
entity .doWithAssociations ((SimpleAssociationHandler ) association -> {
419
420
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
+ });
423
426
return associationsFields ;
424
427
}
425
428
@@ -438,13 +441,31 @@ private List<String> getIgnoredFields(ResourceMetadata
438
441
ignoredFields .add (idField );
439
442
entity .doWithAssociations ((SimpleAssociationHandler ) association -> {
440
443
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
+ });
444
449
}
445
450
return ignoredFields ;
446
451
}
447
452
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
+
448
469
/**
449
470
* Build text uri content.
450
471
*
0 commit comments