Skip to content

Conversation

@btecu
Copy link

@btecu btecu commented May 22, 2018

This is how I have it working in my project. I never set it the way it's currently documented and I'm not completely sure if there's any difference.

Both input and output work for me.

@svschannak
Copy link

@btecu Looks promising. But i get the following error:
IServiceCollection' does not contain a definition for 'AddJsonOptions' and the best extension method overload 'MvcJsonMvcBuilderExtensions.AddJsonOptions(IMvcBuilder, Action<MvcJsonOptions>)' requires a receiver of type 'IMvcBuilder'

Do you have any idea where this comes from?

@btecu
Copy link
Author

btecu commented Jan 24, 2019

@svschannak are you importing Microsoft.Extensions.Configuration.Json?

@alex-davies
Copy link
Collaborator

I think the AddJsonOptions should be on the AddMvc()

i.e services.AddMvc().AddJsonOptions(x => x.SerializerSettings.ContractResolver = new JsonApiSerializerSettings().ContractResolver);

It is a pretty slick way to configure it, certainly a lot simpler than the currently documented way. If you update the PR with the correct code and include the using statement ill merge

@craigmccauley
Copy link

The generated Json is incorrect when rendered by my Action when I do it this way.

public class Sample {
    public string Id{get;set;}
    public RelatedObject MyObj{get;set;}
}
public class RelatedObject{
    public string Id{get;set;}
    public string Name{get;set;}
}

[HttpGet]
public ActionResult<DocumentRoot<Sample>> GetSample(){
    return Ok(new DocumentRoot<Sample>(){
        Data = new Sample(){
            Id = "1",
            MyObj = new RelatedObject() { Id = "2" }
        }
    });
}

//expected
{
  "data": {
    "id": "1",
    "type": "sample",
    "relationships": {
      "myObj": {
        "data": {
          "id": "2",
          "type": "relatedobject"
        }
      }
    }
  }
}

//actual
{
  "data": {
    "id": "1",
    "type": "sample",
    "relationships": {
      "myObj": {
        "data": {
          "id": "2",
          "type": "relatedobject"
        }
      }
    }
  },
  "included": [
    {
      "id": "2",
      "type": "relatedobject",
      "attributes": {
        "name": null
      }
    }
  ]
}

The method here #35 (comment) works as expected for me.

@chrisketelaar
Copy link

chrisketelaar commented Apr 3, 2019

I also developed an alternative way for integrating with AspNetCore.

https://github.com/chrisketelaar/JsonApiSerializer/tree/master/src/JsonApiSerializer.AspNetCore.Mvc.Formatters.JsonApi

With this you can do:
services.AddMvc()
.AddJsonApi(options => { options.AllowInputFormatterExceptionMessages = false })

I created a fork and added a new project to keep the core project clean of other external dependencies.

@alex-davies What do you think? (tag @btecu )

edit:

With this you can do content negotiation (https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.2#content-negotiation) based on the Accept header provided by the caller.

So instead of overriding the JsonFormatter implementation, a new JsonApiFormatter is added which will respond on application/vnd.api+json and the original JsonFormatter will respond on application/json.

Ofcourse Mvc allows you to force a particular format (https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.2#forcing-a-particular-format)

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

Successfully merging this pull request may close these issues.

5 participants