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

Can't POST Id as Number (without quotes) #108

Open
wwdenis opened this issue Sep 6, 2019 · 2 comments
Open

Can't POST Id as Number (without quotes) #108

wwdenis opened this issue Sep 6, 2019 · 2 comments

Comments

@wwdenis
Copy link

wwdenis commented Sep 6, 2019

Summary

It's not possible to to POST/PUT a body with Id as a number (without quotes).

Steps to Reproduce

JSONAPI_ISSUE

Reason

I am using Swashbuckle. to for documentation, and Ids are documented without quotes.

Configuration

  • TargetFramework: netcoreapp2.2
  • JsonApiSerialiser: 1.7.3
  • Newtonsoft.Json: 12.0.2

Sample Code

Model

class CityModel 
{
    public int Id { get; set; }
    public string Type { get; set; }
    public string Name { get; set; }
}

Controller

[Route("api/cities")]
[ApiController]
public class CityController : ControllerBase
{
    [HttpGet("{id}")]
    public IActionResult Get(int id)
    {
        var item = new CityModel
        {
            Id = id,
            Name = "Test"
        };

        return Ok(item);
    }

    [HttpPost]
    public IActionResult Post([FromBody] CityModel model)
    {
        return Ok($"RECEIVED: {model.Id}");
    }
}

Startup

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(opt => EnableJsonApi(opt, services));
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }

    private static void EnableJsonApi(MvcOptions options, IServiceCollection services)
    {
        var serviceProvider = services.BuildServiceProvider();
        var objectPoolProvider = serviceProvider.GetService<ObjectPoolProvider>();
        var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
        var logger = loggerFactory.CreateLogger<JsonInputFormatter>();

        var serializerSettings = new JsonApiSerializerSettings();
        var charPool = ArrayPool<char>.Shared;

        var outputFormatter = new JsonOutputFormatter(serializerSettings, charPool);
        var inputFormatter = new JsonInputFormatter(logger, serializerSettings, charPool, objectPoolProvider, options, null);

        options.OutputFormatters.RemoveType<JsonOutputFormatter>();
        options.InputFormatters.RemoveType<JsonInputFormatter>();

        options.OutputFormatters.Insert(0, outputFormatter);
        options.InputFormatters.Insert(0, inputFormatter);
    }
}
@alex-davies
Copy link
Collaborator

The json:api spec explicitly states id must be a string (https://jsonapi.org/format/#document-resource-object-identification) so passing id as an int the json is not technically valid json:api.

The values of the id and type members MUST be strings.

There was a feature where we allow the serializer to handle int id on the class and we handle the mappings to/from string for you, but it doesn't change the format of id on the wire

@AndreSteenveld
Copy link

Event though this is a ticket about the identifier in a json-api document this is the most complete example of JsonApiSerializer I could find.

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

3 participants