Skip to content

Commit

Permalink
Only exported association properties should be ignored in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
clementdenis committed Feb 15, 2024
1 parent a714f41 commit 765607d
Showing 1 changed file with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.SimpleAssociationHandler;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
Expand Down Expand Up @@ -133,7 +134,7 @@ public void customise(OpenAPI openAPI, ResourceMappings mappings, PersistentEnti
entityInfo.setAssociationsFields(associationsFields);
entityInoMap.put(domainType.getSimpleName(), entityInfo);
}

openAPI.getPaths().entrySet().stream()
.forEach(stringPathItemEntry -> {
PathItem pathItem = stringPathItemEntry.getValue();
Expand Down Expand Up @@ -417,9 +418,11 @@ private List<String> getAssociationsFields(ResourceMetadata
List<String> associationsFields = new ArrayList<>();
entity.doWithAssociations((SimpleAssociationHandler) association -> {
PersistentProperty<?> property = association.getInverse();
String filedName = resourceMetadata.getMappingFor(property).getRel().value();
associationsFields.add(filedName);
});
if (isAssociationExported(property)) {
String filedName = resourceMetadata.getMappingFor(property).getRel().value();
associationsFields.add(filedName);
}
});
return associationsFields;
}

Expand All @@ -438,13 +441,31 @@ private List<String> getIgnoredFields(ResourceMetadata
ignoredFields.add(idField);
entity.doWithAssociations((SimpleAssociationHandler) association -> {
PersistentProperty<?> property = association.getInverse();
String filedName = resourceMetadata.getMappingFor(property).getRel().value();
ignoredFields.add(filedName);
});
if (isAssociationExported(property)) {
String filedName = resourceMetadata.getMappingFor(property).getRel().value();
ignoredFields.add(filedName);
}
});
}
return ignoredFields;
}

/**
* Indicates if an association is exported. If not, it should be included in response schema.
*
* @param associationProperty the property to check
* @return true if a field is not exported
*/
private boolean isAssociationExported(PersistentProperty<?> associationProperty) {
try {
RestResource restResource = associationProperty.getRequiredAnnotation(RestResource.class);
return restResource.exported();
} catch (IllegalStateException e) {
//if annotation missing, association is exported by default
return true;
}
}

/**
* Build text uri content.
*
Expand Down

0 comments on commit 765607d

Please sign in to comment.