Skip to content
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

Alba's built in JSON serialization isn't working for oData endpoints #116

Open
jeremydmiller opened this issue Oct 31, 2022 · 3 comments
Open

Comments

@jeremydmiller
Copy link
Member

More research necessary

@ticky74
Copy link

ticky74 commented Jun 21, 2023

Pretty new to Alba, however I have been using it successfully against OData endpoints with the AspVersioning library. I think there is a bit of a nuance, I'm using [email protected]. Although the OData requests go through controllers, the serialization aligns with the MinimalApiStrategy. In the AlbaHost constructors, the input/output formatters are of type ODataInputFormatter and ODataOutputFormatter, so the MvcStrategy is initialized.

var jsonInput  = findInputFormatter("application/json");
var jsonOutput = findOutputFormatter("application/json");

if (jsonInput != null && jsonOutput != null)
{
    MvcStrategy = new FormatterSerializer(this, jsonInput, jsonOutput);
}

MinimalApiStrategy = new SystemTextJsonSerializer(this);

DefaultJson = MvcStrategy ?? MinimalApiStrategy;

So the DefaultJson is then set to the MvcStrategy and I guess OData isn't a fan.

I have not really tested this against the library, I just grabbed the source and hacked in a couple overloaded constructors and For<>... methods and so far things have been working. Not sure if this helps anyone, or not, but I too had the pleasure of banging my head against the wall with the Asp.Versioning and OData libraries so thought I'd post just in case!

@yulivee
Copy link

yulivee commented Nov 28, 2023

@ticky74 Would you mind sharing your Hack?
Struggeling with a similar Problem, using [email protected] as a transitive dependency of the ResTier [email protected]. When Posting Alba supports handing over the MinimalApiStrategy, so we can successfully post json - but when trying to use the ReadAsJson Methods, we get Deserialization Exceptions. Currently stuck testing with ReadAsText which is crude in comparison

@vcaraulean
Copy link

I've ran into same issue. Our project has a mix of OData and API controllers.
Following snippet:

        await fixture.Host.Scenario(x =>
        {
            x.Post
                .Json(new { name = DocumentName, body })
                .ToUrl("/documents/wiki");
        
            x.StatusCodeShouldBeOk();
        });

Throws this error:

System.UriFormatException
Invalid URI: The format of the URI could not be determined.
   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
   at System.Uri..ctor(String uriString)
   at Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatter.GetDefaultBaseAddress(HttpRequest request)
   at Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatter.GetBaseAddressInternal(HttpRequest request)
   at Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
   at Microsoft.AspNetCore.Mvc.Formatters.TextOutputFormatter.WriteAsync(OutputFormatterWriteContext context)
   at Alba.Serialization.FormatterSerializer.Write[T](T body) in /_/src/Alba/Serialization/FormatterSerializer.cs:line 34
   at Alba.Scenario.<>c__DisplayClass44_0`1.<WriteJson>b__0(HttpContext c) in /_/src/Alba/Scenario.cs:line 250
   at Alba.Scenario.SetupHttpContext(HttpContext context) in /_/src/Alba/Scenario.cs:line 346

A temporary workaround that seems to be working for us is to fall back to using HttpClient:

        using var httpClient = fixture.Host.GetTestServer().CreateClient();
        var response = await httpClient.PostAsync("/documents/wiki", 
                                                  JsonContent.Create(new { name = DocumentName, body }));
        response.EnsureSuccessStatusCode();

A proper fix might require some effort to pick up the right formatter depending on individual request...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants