Open Closed

GuidGenerator error #5758


User avatar
0
mmaldonado@emscltd.com created
  • ABP Framework version: 6.0.0.0
  • UI Type: Angular
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes
  • Steps to reproduce the issue:

Error on generating GUID, portion of code from repository to set Id

Class EfCoreRepository

`protected virtual void CheckAndSetId(TEntity entity) { if (entity is IEntity

protected virtual void TrySetGuidId(IEntity<Guid> entity)
{
    if (entity.Id != default)
    {
        return;
    }

    EntityHelper.TrySetId(
        entity,
        () => GuidGenerator.Create(),
        true
    );
}`

the guid generated does not match the pattern for GUIDs [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12} based on https://datatracker.ietf.org/doc/html/rfc4122

Partial solution

public class GuidValueGenerator : ValueGenerator<Guid> { private readonly IGuidGenerator _guidGenerator; private readonly string pattern = @"^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$";

public override bool GeneratesTemporaryValues => false;

public GuidValueGenerator()
{
    var options = Options.Create(new AbpSequentialGuidGeneratorOptions());
    options.Value.DefaultSequentialGuidType = SequentialGuidType.SequentialAtEnd;

    _guidGenerator = new SequentialGuidGenerator(options);
}


public override Guid Next(EntityEntry entry)
{
    var result = GenerateNextValue();

    string json = JsonConvert.SerializeObject(entry.Entity);

    WriteOnText(result.ToString());
    WriteOnText(json);

    return result;
}

public Guid GenerateNextValue()
{
    var result = _guidGenerator.Create();

    if (!Regex.IsMatch(result.ToString(), pattern))
    {
        return GenerateNextValue();
    }

    return result;
}

public void WriteOnText(string newGuid)
{
    string filePath = @"D:\guid-lines.txt"; // Replace with your file path

    // Open the file for writing
    using (StreamWriter writer = new StreamWriter(filePath, true))
    {
        writer.WriteLine(newGuid);
    }
}

}


7 Answer(s)
  • User Avatar
    0
    mmaldonado@emscltd.com created

    Let me know when you fix this problem please to delete my own implementation

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    the guid generated does not match the pattern for GUIDs

    This is abp implement.

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Guids/Volo/Abp/Guids/SequentialGuidGenerator.cs#L14

    If you think there is an error you can replace it with your own. : )

  • User Avatar
    0
    ElevosDev created

    I was using yours, and it was generating not valid GUIDs

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    It doesn't match the https://datatracker.ietf.org/doc/html/rfc4122

    But there should be no error.

  • User Avatar
    0
    ElevosDev created

    from 100 just 15 pass, I have to connect to other systems that validate the Id of some elements, so, all my Ids has to pass

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    That is why I suggest you replace the default implement.

    I have refunded your question credits.

  • User Avatar
    0
    ElevosDev created

    As I say, the default implementation generates this problem, just test it yourself.

    Thank you very much for the credits, I appreciate it

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11