Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ project
----------------------------------------------------------------------------------------------------- | ------------
[SysML2.NET](https://www.nuget.org/packages/SysML2.NET) | ![NuGet Version](https://img.shields.io/nuget/v/SysML2.NET)
[SysML2.NET.Extensions](https://www.nuget.org/packages/SysML2.NET.Extensions) | ![NuGet Version](https://img.shields.io/nuget/v/SysML2.NET.Extensions)
[SysML2.NET.Identifiers](https://www.nuget.org/packages/SysML2.NET.Identifiers) | ![NuGet Version](https://img.shields.io/nuget/v/SysML2.NET.Identifiers)
[SysML2.NET.Semantics](https://www.nuget.org/packages/SysML2.NET.Semantics) | ![NuGet Version](https://img.shields.io/nuget/v/SysML2.NET.Semantics)
[SysML2.NET.Serializer.Json](https://www.nuget.org/packages/SysML2.NET.Serializer.Json) | ![NuGet Version](https://img.shields.io/nuget/v/SysML2.NET.Serializer.Json)
[SysML2.NET.Serializer.Xmi](https://www.nuget.org/packages/SysML2.NET.Serializer.Xmi) | ![NuGet Version](https://img.shields.io/nuget/v/SysML2.NET.Serializer.Xmi)
Expand Down Expand Up @@ -54,7 +55,7 @@ Every NuGet version is declared once, in [Directory.Packages.props](Directory.Pa

The redistributable libraries multi-target `netstandard2.1;net10.0`, so a referenced version is a **minimum floor** for every downstream consumer. The version policy follows from that:

- **Libraries** (`SysML2.NET`, `SysML2.NET.Serializer.*`, `SysML2.NET.Dal`, `SysML2.NET.REST`, `SysML2.NET.Kpar`, `SysML2.NET.Extensions`, `SysML2.NET.Semantics`, `SysML2.NET.Validation`) reference the lowest version that provides the APIs used. Stable framework abstractions stay at an LTS floor; serialization and cryptography packages track current.
- **Libraries** (`SysML2.NET`, `SysML2.NET.Serializer.*`, `SysML2.NET.Dal`, `SysML2.NET.REST`, `SysML2.NET.Kpar`, `SysML2.NET.Extensions`, `SysML2.NET.Identifiers`, `SysML2.NET.Semantics`, `SysML2.NET.Validation`) reference the lowest version that provides the APIs used. Stable framework abstractions stay at an LTS floor; serialization and cryptography packages track current.
- **Applications and test projects** track current versions.
- A library dependency is raised only when a required new API needs it, or when a CVE affects that package.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ CREATE INDEX ix_data_identity_project_kind ON sysml2.data_identity (project_id,

CREATE TABLE sysml2.element_version (
project_id uuid NOT NULL,
version_id uuid NOT NULL, -- DataVersion.id
version_id uuid NOT NULL DEFAULT gen_random_uuid(), -- DataVersion.id: random, version 4 (API Clause 7.1.2)
identity_id uuid NOT NULL, -- FK via the composite typed-identity check below
commit_id uuid NOT NULL REFERENCES sysml2.commit (id),
class_kind smallint NOT NULL REFERENCES sysml2.class_kind (id),
Expand Down Expand Up @@ -4274,21 +4274,22 @@ $$;

-- PG18 OPPORTUNISTIC DEFAULTS (version-guarded; a no-op on the PostgreSQL 16/17 floor).
--
-- On PostgreSQL 18+ the schema self-activates native uuidv7() defaults on every
-- SERVER-MINTED key. Time-ordered ids turn each project's insert pattern into rightmost
-- btree appends instead of random-page scatter (audit finding R8). The application-side
-- Guid.CreateVersion7() remains the PRIMARY id source — the service usually needs the ids
-- On PostgreSQL 18+ the schema self-activates native uuidv7() defaults on the server-minted
-- keys that may be time-ordered. Time-ordered ids turn each project's insert pattern into
-- rightmost btree appends, not random-page scatter (audit finding R8). The application-side
-- Identifier.NewId() remains the PRIMARY id source — the service usually needs the ids
-- before insert (to wire derived_id into branch_head etc.); these defaults are the safety
-- net that keeps ad-hoc/tooling inserts time-ordered too.
--
-- Deliberately NOT defaulted: data_identity.id (the spec-visible @id is supplied by the
-- API layer or the client — a silent server default would mask missing-id bugs).
-- Deliberately NOT defaulted here: element_version.version_id (DataVersion.id is a random,
-- version 4 UUID, API Clause 7.1.2 — defaulted to gen_random_uuid() in §5) and
-- data_identity.id (the spec-visible @id is supplied by the API layer or the client — a
-- silent server default would mask missing-id bugs).
DO $$
BEGIN

IF current_setting('server_version_num')::int >= 180000 THEN

ALTER TABLE sysml2.element_version ALTER COLUMN version_id SET DEFAULT uuidv7();
ALTER TABLE sysml2.derived_version ALTER COLUMN derived_id SET DEFAULT uuidv7();
ALTER TABLE sysml2.project ALTER COLUMN id SET DEFAULT uuidv7();
ALTER TABLE sysml2.commit ALTER COLUMN id SET DEFAULT uuidv7();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void VerifySchemaStructureQueries()
Assert.That(() => ((IOpenApiSchema)null).QueryUnionAlternativeNames(), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => ((IOpenApiSchema)null).QueryTypeDiscriminator(), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => ((IOpenApiSchema)null).QueryHasRequiredIdentifier(), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => ((IOpenApiSchema)null).QueryHasOptionalIdentifier(), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => ((IOpenApiSchema)null).QueryMappableProperties("Project"), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => this.schemas["Project"].QueryMappableProperties(null), Throws.TypeOf<ArgumentNullException>());

Expand All @@ -62,6 +63,12 @@ public void VerifySchemaStructureQueries()
Assert.That(this.schemas["PrimitiveConstraint"].QueryHasRequiredIdentifier(), Is.False);
Assert.That(this.schemas["ProjectUsageRequest"].QueryHasRequiredIdentifier(), Is.False);

Assert.That(this.schemas["ProjectUsageRequest"].QueryHasOptionalIdentifier(), Is.True);
Assert.That(this.schemas["ExternalDataRequest"].QueryHasOptionalIdentifier(), Is.True);
Assert.That(this.schemas["Project"].QueryHasOptionalIdentifier(), Is.False);
Assert.That(this.schemas["DataIdentityRequest"].QueryHasOptionalIdentifier(), Is.False);
Assert.That(this.schemas["Error"].QueryHasOptionalIdentifier(), Is.False);

Assert.That(this.schemas["Project"].QueryTypeDiscriminator(), Is.EqualTo("Project"));
Assert.That(this.schemas["ProjectRequest"].QueryTypeDiscriminator(), Is.EqualTo("Project"));
Assert.That(this.schemas["Data"].QueryTypeDiscriminator(), Is.Null);
Expand Down
17 changes: 9 additions & 8 deletions SysML2.NET.CodeGenerator.Tests/Sql/schema.golden.sql
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ CREATE INDEX ix_data_identity_project_kind ON sysml2.data_identity (project_id,

CREATE TABLE sysml2.element_version (
project_id uuid NOT NULL,
version_id uuid NOT NULL, -- DataVersion.id
version_id uuid NOT NULL DEFAULT gen_random_uuid(), -- DataVersion.id: random, version 4 (API Clause 7.1.2)
identity_id uuid NOT NULL, -- FK via the composite typed-identity check below
commit_id uuid NOT NULL REFERENCES sysml2.commit (id),
class_kind smallint NOT NULL REFERENCES sysml2.class_kind (id),
Expand Down Expand Up @@ -1897,21 +1897,22 @@ $$;

-- PG18 OPPORTUNISTIC DEFAULTS (version-guarded; a no-op on the PostgreSQL 16/17 floor).
--
-- On PostgreSQL 18+ the schema self-activates native uuidv7() defaults on every
-- SERVER-MINTED key. Time-ordered ids turn each project's insert pattern into rightmost
-- btree appends instead of random-page scatter (audit finding R8). The application-side
-- Guid.CreateVersion7() remains the PRIMARY id source — the service usually needs the ids
-- On PostgreSQL 18+ the schema self-activates native uuidv7() defaults on the server-minted
-- keys that may be time-ordered. Time-ordered ids turn each project's insert pattern into
-- rightmost btree appends, not random-page scatter (audit finding R8). The application-side
-- Identifier.NewId() remains the PRIMARY id source — the service usually needs the ids
-- before insert (to wire derived_id into branch_head etc.); these defaults are the safety
-- net that keeps ad-hoc/tooling inserts time-ordered too.
--
-- Deliberately NOT defaulted: data_identity.id (the spec-visible @id is supplied by the
-- API layer or the client — a silent server default would mask missing-id bugs).
-- Deliberately NOT defaulted here: element_version.version_id (DataVersion.id is a random,
-- version 4 UUID, API Clause 7.1.2 — defaulted to gen_random_uuid() in §5) and
-- data_identity.id (the spec-visible @id is supplied by the API layer or the client — a
-- silent server default would mask missing-id bugs).
DO $$
BEGIN

IF current_setting('server_version_num')::int >= 180000 THEN

ALTER TABLE sysml2.element_version ALTER COLUMN version_id SET DEFAULT uuidv7();
ALTER TABLE sysml2.derived_version ALTER COLUMN derived_id SET DEFAULT uuidv7();
ALTER TABLE sysml2.project ALTER COLUMN id SET DEFAULT uuidv7();
ALTER TABLE sysml2.commit ALTER COLUMN id SET DEFAULT uuidv7();
Expand Down
13 changes: 13 additions & 0 deletions SysML2.NET.CodeGenerator/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ public static bool QueryHasRequiredIdentifier(this IOpenApiSchema schema)
&& schema.Required?.Contains(IdentifierPropertyName) == true;
}

/// <summary>
/// Queries whether the schema declares an identifier that it does not list as required
/// </summary>
/// <param name="schema">The subject <see cref="IOpenApiSchema"/>.</param>
/// <returns>True when the identifier is declared and optional.</returns>
public static bool QueryHasOptionalIdentifier(this IOpenApiSchema schema)
{
ArgumentNullException.ThrowIfNull(schema);

return schema.Properties?.ContainsKey(IdentifierPropertyName) == true
&& schema.Required?.Contains(IdentifierPropertyName) != true;
}

/// <summary>
/// Queries the properties of the schema that can be expressed as C# properties
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ private static IEnumerable<string> QueryFamilyInterfaces(string schemaName, IOpe
{
yield return "SysML2.NET.Common.IIdentified";
}

if (schema.QueryHasOptionalIdentifier())
{
yield return "IOptionallyIdentified";
}
}

/// <summary>
Expand Down
17 changes: 9 additions & 8 deletions SysML2.NET.CodeGenerator/Templates/Uml/core-sql-schema.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ CREATE INDEX ix_data_identity_project_kind ON sysml2.data_identity (project_id,

CREATE TABLE sysml2.element_version (
project_id uuid NOT NULL,
version_id uuid NOT NULL, -- DataVersion.id
version_id uuid NOT NULL DEFAULT gen_random_uuid(), -- DataVersion.id: random, version 4 (API Clause 7.1.2)
identity_id uuid NOT NULL, -- FK via the composite typed-identity check below
commit_id uuid NOT NULL REFERENCES sysml2.commit (id),
class_kind smallint NOT NULL REFERENCES sysml2.class_kind (id),
Expand Down Expand Up @@ -1255,21 +1255,22 @@ $$;

-- PG18 OPPORTUNISTIC DEFAULTS (version-guarded; a no-op on the PostgreSQL 16/17 floor).
--
-- On PostgreSQL 18+ the schema self-activates native uuidv7() defaults on every
-- SERVER-MINTED key. Time-ordered ids turn each project's insert pattern into rightmost
-- btree appends instead of random-page scatter (audit finding R8). The application-side
-- Guid.CreateVersion7() remains the PRIMARY id source — the service usually needs the ids
-- On PostgreSQL 18+ the schema self-activates native uuidv7() defaults on the server-minted
-- keys that may be time-ordered. Time-ordered ids turn each project's insert pattern into
-- rightmost btree appends, not random-page scatter (audit finding R8). The application-side
-- Identifier.NewId() remains the PRIMARY id source — the service usually needs the ids
-- before insert (to wire derived_id into branch_head etc.); these defaults are the safety
-- net that keeps ad-hoc/tooling inserts time-ordered too.
--
-- Deliberately NOT defaulted: data_identity.id (the spec-visible @id is supplied by the
-- API layer or the client — a silent server default would mask missing-id bugs).
-- Deliberately NOT defaulted here: element_version.version_id (DataVersion.id is a random,
-- version 4 UUID, API Clause 7.1.2 — defaulted to gen_random_uuid() in §5) and
-- data_identity.id (the spec-visible @id is supplied by the API layer or the client — a
-- silent server default would mask missing-id bugs).
DO $$
BEGIN

IF current_setting('server_version_num')::int >= 180000 THEN

ALTER TABLE sysml2.element_version ALTER COLUMN version_id SET DEFAULT uuidv7();
ALTER TABLE sysml2.derived_version ALTER COLUMN derived_id SET DEFAULT uuidv7();
ALTER TABLE sysml2.project ALTER COLUMN id SET DEFAULT uuidv7();
ALTER TABLE sysml2.commit ALTER COLUMN id SET DEFAULT uuidv7();
Expand Down
17 changes: 9 additions & 8 deletions SysML2.NET.Dal/AutoGenSql/0001_initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ CREATE INDEX ix_data_identity_project_kind ON sysml2.data_identity (project_id,

CREATE TABLE sysml2.element_version (
project_id uuid NOT NULL,
version_id uuid NOT NULL, -- DataVersion.id
version_id uuid NOT NULL DEFAULT gen_random_uuid(), -- DataVersion.id: random, version 4 (API Clause 7.1.2)
identity_id uuid NOT NULL, -- FK via the composite typed-identity check below
commit_id uuid NOT NULL REFERENCES sysml2.commit (id),
class_kind smallint NOT NULL REFERENCES sysml2.class_kind (id),
Expand Down Expand Up @@ -4274,21 +4274,22 @@ $$;

-- PG18 OPPORTUNISTIC DEFAULTS (version-guarded; a no-op on the PostgreSQL 16/17 floor).
--
-- On PostgreSQL 18+ the schema self-activates native uuidv7() defaults on every
-- SERVER-MINTED key. Time-ordered ids turn each project's insert pattern into rightmost
-- btree appends instead of random-page scatter (audit finding R8). The application-side
-- Guid.CreateVersion7() remains the PRIMARY id source — the service usually needs the ids
-- On PostgreSQL 18+ the schema self-activates native uuidv7() defaults on the server-minted
-- keys that may be time-ordered. Time-ordered ids turn each project's insert pattern into
-- rightmost btree appends, not random-page scatter (audit finding R8). The application-side
-- Identifier.NewId() remains the PRIMARY id source — the service usually needs the ids
-- before insert (to wire derived_id into branch_head etc.); these defaults are the safety
-- net that keeps ad-hoc/tooling inserts time-ordered too.
--
-- Deliberately NOT defaulted: data_identity.id (the spec-visible @id is supplied by the
-- API layer or the client — a silent server default would mask missing-id bugs).
-- Deliberately NOT defaulted here: element_version.version_id (DataVersion.id is a random,
-- version 4 UUID, API Clause 7.1.2 — defaulted to gen_random_uuid() in §5) and
-- data_identity.id (the spec-visible @id is supplied by the API layer or the client — a
-- silent server default would mask missing-id bugs).
DO $$
BEGIN

IF current_setting('server_version_num')::int >= 180000 THEN

ALTER TABLE sysml2.element_version ALTER COLUMN version_id SET DEFAULT uuidv7();
ALTER TABLE sysml2.derived_version ALTER COLUMN derived_id SET DEFAULT uuidv7();
ALTER TABLE sysml2.project ALTER COLUMN id SET DEFAULT uuidv7();
ALTER TABLE sysml2.commit ALTER COLUMN id SET DEFAULT uuidv7();
Expand Down
87 changes: 87 additions & 0 deletions SysML2.NET.Identifiers.Tests/IdentifierTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// ------------------------------------------------------------------------------------------------
// <copyright file="IdentifierTestFixture.cs" company="Starion Group S.A.">
//
// Copyright (C) 2022-2026 Starion Group S.A.
// SPDX-License-Identifier: Apache-2.0
//
// </copyright>
// ------------------------------------------------------------------------------------------------

namespace SysML2.NET.Identifiers.Tests
{
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;

using NUnit.Framework;

[TestFixture]
public class IdentifierTestFixture
{
private const string VariantDigits = "89ab";

[OneTimeSetUp]
public void OneTimeSetUp()
{
var targetFramework = typeof(Identifier).Assembly.GetCustomAttribute<TargetFrameworkAttribute>();

Assert.That(targetFramework?.FrameworkName, Is.EqualTo(".NETStandard,Version=v2.1"));
}

[Test]
public void VerifyNewId()
{
var before = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var identifier = Identifier.NewId();
var after = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

var text = identifier.ToString();

using (Assert.EnterMultipleScope())
{
Assert.That(text[14], Is.EqualTo('7'));
Assert.That(VariantDigits, Does.Contain(text[19]));
Assert.That(TimestampOf(identifier), Is.InRange(before, after));
Assert.That(Identifier.NewId(), Is.Not.EqualTo(identifier));
}
}

[Test]
public void VerifyCreateVersion7()
{
var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(0x0123_4567_89AB);
var identifier = Identifier.CreateVersion7(timestamp);
var text = identifier.ToString();

using (Assert.EnterMultipleScope())
{
Assert.That(text, Does.StartWith("01234567-89ab-7"));
Assert.That(VariantDigits, Does.Contain(text[19]));
Assert.That(Identifier.CreateVersion7(timestamp), Is.Not.EqualTo(identifier));
}

var latest = Identifier.CreateVersion7(DateTimeOffset.MaxValue);

Assert.That(latest.ToString(), Does.StartWith("e677d21f-dbff-7"));

var later = Identifier.CreateVersion7(timestamp.AddMilliseconds(1));
var earlier = Identifier.CreateVersion7(timestamp.AddMilliseconds(-1));

Guid[] inCreationOrder = [earlier, identifier, later, latest];
Guid[] shuffled = [latest, identifier, earlier, later];

using (Assert.EnterMultipleScope())
{
Assert.That(shuffled.Order(), Is.EqualTo(inCreationOrder));
Assert.That(shuffled.Select(guid => guid.ToString()).Order(StringComparer.Ordinal), Is.EqualTo(inCreationOrder.Select(guid => guid.ToString())));
}
}

private static long TimestampOf(Guid identifier)
{
return long.Parse(identifier.ToString("N")[..12], NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
}
}
Loading
Loading