codecutout / JsonApiSerializer

JsonApiSerializer supports configurationless serializing and deserializing objects into the json:api format (http://jsonapi.org).
MIT License
113 stars 46 forks source link

Expected to find string at data[0].id. The value of 'id' MUST be a string #128

Open gokeiyou opened 3 years ago

gokeiyou commented 3 years ago

An exception is thrown when the application is running, Expected to find string at data[0].id. The value of'id' MUST be a string, but the type of ID in the system is int, how to get out of this situation?

iskpups88 commented 3 years ago

By json:api specification id must be a string: https://github.com/codecutout/JsonApiSerializer/issues/108#issuecomment-529234791

oestrada1001 commented 1 year ago

I'm having the same issue. I'm using the JsonApiDotNetCore NuGet package in the backend.

I have two models:

[Resource]
public class ExerciseType : Identifiable<int>
{
    [Attr]
    public override int Id { get; set; }

    [Attr, Required]
    public string Name { get; set; }
}

[Resource]
public class Exercise : Identifiable<int>
{
    [Attr]
    public override int Id { get; set; }

    [Attr, Required]
    public string Name { get; set; }

    [Attr, AllowNull]
    public string? ImageUrl { get; set; }

    [Attr, Required]
    public string Technique { get; set; }

    [Attr, Required]
    public ExerciseType ExerciseType { get; set; }

    [HasMany]
    public ICollection<Equipment> Equipment { get; set; }

    [HasMany]
    public ICollection<PrimaryMuscleExercise> PrimaryMuscleExercises { get; set; }

    [HasMany]
    public ICollection<SecondaryMuscleExercise> SecondaryMuscleExercises { get; set; }

    [HasMany]
    public ICollection<AlternativeExerciseName> AlternativeExerciseNames { get; set; }
}

The problem seems to be coming from the json its the package is returning to the front end.

{
"data":[
      {
         "type":"exercises",
         "id":"1",
         "attributes":{
            "name":"Dumbbell Curls",
            "imageUrl":"https://www.shutterstock.com/image-illustration/one-arm-dumbbell-preacher-curl-600w-430936294.jpg",
            "technique":"Stand with your feet shoulder-width apart and hold a dumbbell in each hand at arm's length by your sides.\n\nKeep your elbows close to your body and your palms facing forward.\n\nSlowly lift the dumbbells by bending your elbows and bringing them towards your shoulders.\n\nPause at the top of the movement for a moment before slowly lowering the dumbbells back to the starting position.\n\nRepeat for the desired number of repetitions.",
            "exerciseType":{
               "id":2, <---------------------- Here
               "name":"Isolation",
               "stringId":"2",
               "localId":null
            }
         },
         "relationships":{
            "equipment":{
               "links":{
                  "self":"http://127.0.0.1:5280/exercises/1/relationships/equipment",
                  "related":"http://127.0.0.1:5280/exercises/1/equipment"
               }
            },
            "primaryMuscleExercises":{
               "links":{
                  "self":"http://127.0.0.1:5280/exercises/1/relationships/primaryMuscleExercises",
                  "related":"http://127.0.0.1:5280/exercises/1/primaryMuscleExercises"
               }
            },
            "secondaryMuscleExercises":{
               "links":{
                  "self":"http://127.0.0.1:5280/exercises/1/relationships/secondaryMuscleExercises",
                  "related":"http://127.0.0.1:5280/exercises/1/secondaryMuscleExercises"
               }
            },
            "alternativeExerciseNames":{
               "links":{
                  "self":"http://127.0.0.1:5280/exercises/1/relationships/alternativeExerciseNames",
                  "related":"http://127.0.0.1:5280/exercises/1/alternativeExerciseNames"
               }
            }
         },
         "links":{
            "self":"http://127.0.0.1:5280/exercises/1"
         }
      },
}

Any suggestions on how I can get around this?