Skip to content

Fix OnReturn secondary resource pipeline for HasOne relationships #929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public override void BeforeImplicitUpdateRelationship(IRelationshipsDictionary<P
resourcesByRelationship.GetByRelationship<Person>().ToList().ForEach(kvp => DoesNotTouchLockedPassports(kvp.Value));
}

public override IEnumerable<Passport> OnReturn(HashSet<Passport> resources, ResourcePipeline pipeline)
{
return resources.Where(p => !p.IsLocked);
}

private void DoesNotTouchLockedPassports(IEnumerable<Passport> resources)
{
foreach (var passport in resources ?? Enumerable.Empty<Passport>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ public object OnReturnRelationship(object resourceOrResources)
return _resourceHookExecutor.OnReturn(resources, ResourcePipeline.GetRelationship).ToArray();
}

if (resourceOrResources is IIdentifiable identifiable)
if (resourceOrResources is IIdentifiable)
{
var resources = ToList(identifiable);
return _resourceHookExecutor.OnReturn(resources, ResourcePipeline.GetRelationship).Single();
var resources = ToList((dynamic)resourceOrResources);
return Enumerable.SingleOrDefault(_resourceHookExecutor.OnReturn(resources, ResourcePipeline.GetRelationship));
}

return resourceOrResources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,29 @@ public async Task Article_Through_Secondary_Endpoint_Is_Hidden()
Assert.DoesNotContain(toBeExcluded, body);
}

[Fact]
public async Task Passport_Through_Secondary_Endpoint_Is_Hidden()
{
// Arrange
var person = _personFaker.Generate();
person.Passport = new Passport(_dbContext) {IsLocked = true};

_dbContext.People.Add(person);
await _dbContext.SaveChangesAsync();

var route = $"/api/v1/people/{person.Id}/passport";

// Act
var response = await _client.GetAsync(route);

// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with body: {body}");
var document = JsonConvert.DeserializeObject<Document>(body);
Assert.Null(document.Data);

}

[Fact]
public async Task Tag_Is_Hidden()
{
Expand Down