feat(ontology): 公理 schema 级良构校验(引用存在性+inverseOf 互换+date/datetime)
Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
1 parent
6e08f15581
commit
d6dc45e85f
3 files changed
+196
-12
No files matched your search
@@ -291,14 +291,26 @@ func TestIntegration_AxiomsRoundTrip(t *testing.T) {
|
||||
env, _ := setupTestEnv(t)
|
||||
h := map[string]string{"X-Principal": "manager"}
|
||||
key := env.typeKey
|
||||
// 注意:任务 3 之前未接 schema 良构校验,disjointWith/functionalLinks 仅做"形状存在"。
|
||||
// dateOrder 引用了未声明属性 p1/p2 也会在 create 时报错(任务 3 接入后);
|
||||
// 本测试只用 disjointWith/equivalentTo(无引用,纯形状)。
|
||||
t.Cleanup(func() { env.httpDo(t, "DELETE", "/api/v1/ontology/object-types/"+key, nil, h) })
|
||||
// 任务 3 接入 schema 良构校验后,disjointWith/equivalentTo 引用的类型必须存在。
|
||||
// 提前创建 Other / Alias 两个被引用类型。
|
||||
other, alias := env.typeKey+"_other", env.typeKey+"_alias"
|
||||
t.Cleanup(func() {
|
||||
env.httpDo(t, "DELETE", "/api/v1/ontology/object-types/"+key, nil, h)
|
||||
env.httpDo(t, "DELETE", "/api/v1/ontology/object-types/"+other, nil, h)
|
||||
env.httpDo(t, "DELETE", "/api/v1/ontology/object-types/"+alias, nil, h)
|
||||
})
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/object-types",
|
||||
map[string]any{"api_name": other, "display_name": "Other"}, h); code != 201 {
|
||||
t.Fatalf("create Other failed")
|
||||
}
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/object-types",
|
||||
map[string]any{"api_name": alias, "display_name": "Alias"}, h); code != 201 {
|
||||
t.Fatalf("create Alias failed")
|
||||
}
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/object-types",
|
||||
map[string]any{"api_name": key, "display_name": "公理", "axioms": map[string]any{
|
||||
"disjointWith": []string{"Other"},
|
||||
"equivalentTo": []string{"Alias"},
|
||||
"disjointWith": []string{other},
|
||||
"equivalentTo": []string{alias},
|
||||
}}, h); code != 201 {
|
||||
t.Fatalf("create failed")
|
||||
}
|
||||
@@ -312,7 +324,7 @@ func TestIntegration_AxiomsRoundTrip(t *testing.T) {
|
||||
t.Fatalf("axioms 缺失: %v", ot)
|
||||
}
|
||||
dw, _ := ax["disjointWith"].([]any)
|
||||
if len(dw) != 1 || dw[0] != "Other" {
|
||||
if len(dw) != 1 || dw[0] != other {
|
||||
t.Errorf("disjointWith 回显错: %v", ax)
|
||||
}
|
||||
// 未知键 → 400(严格解析)
|
||||
@@ -322,3 +334,68 @@ func TestIntegration_AxiomsRoundTrip(t *testing.T) {
|
||||
t.Errorf("未知键应 400, got %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
// 各项良构校验的可达性都靠此测试覆盖(任务 3 前 schema 校验尚未接通,全部应 400/合法)。
|
||||
func TestIntegration_AxiomsWellFormed(t *testing.T) {
|
||||
env, _ := setupTestEnv(t)
|
||||
h := map[string]string{"X-Principal": "manager"}
|
||||
keyA, keyB := env.typeKey, env.typeKey+"_b"
|
||||
t.Cleanup(func() {
|
||||
env.httpDo(t, "DELETE", "/api/v1/ontology/object-types/"+keyA, nil, h)
|
||||
env.httpDo(t, "DELETE", "/api/v1/ontology/object-types/"+keyB, nil, h)
|
||||
env.httpDo(t, "DELETE", "/api/v1/ontology/link-types/AtoB", nil, h)
|
||||
env.httpDo(t, "DELETE", "/api/v1/ontology/link-types/BtoA", nil, h)
|
||||
})
|
||||
// 类型 A + 属性 p1 date / p2 datetime
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/object-types",
|
||||
map[string]any{"api_name": keyA, "display_name": "A", "properties": []map[string]any{
|
||||
{"api_name": "p1", "display_name": "P1", "data_type": "date"},
|
||||
{"api_name": "p2", "display_name": "P2", "data_type": "datetime"},
|
||||
}}, h); code != 201 {
|
||||
t.Fatalf("create A failed")
|
||||
}
|
||||
// 类型 B
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/object-types",
|
||||
map[string]any{"api_name": keyB, "display_name": "B"}, h); code != 201 {
|
||||
t.Fatalf("create B failed")
|
||||
}
|
||||
// AtoB(A→B)+ BtoA(B→A)两链接,作为 functionalLinks/inverseOf 测试原料
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/link-types",
|
||||
map[string]any{"api_name": "AtoB", "display_name": "A→B", "source_type": keyA, "target_type": keyB}, h); code != 201 {
|
||||
t.Fatalf("create link AtoB failed")
|
||||
}
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/link-types",
|
||||
map[string]any{"api_name": "BtoA", "display_name": "B→A", "source_type": keyB, "target_type": keyA}, h); code != 201 {
|
||||
t.Fatalf("create link BtoA failed")
|
||||
}
|
||||
|
||||
// 1. disjointWith 引用不存在 → 400
|
||||
if code, _ := env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+keyA,
|
||||
map[string]any{"axioms": map[string]any{"disjointWith": []string{"NoSuchType"}}}, h); code != 400 {
|
||||
t.Errorf("不存在的 disjointWith 应 400, got %d", code)
|
||||
}
|
||||
// 2. functionalLinks 引用 link source_type 非本类型 → 400(BtoA 源=keyB 非 keyA)
|
||||
if code, _ := env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+keyA,
|
||||
map[string]any{"axioms": map[string]any{"functionalLinks": []string{"BtoA"}}}, h); code != 400 {
|
||||
t.Errorf("functionalLinks source_type 不匹配应 400, got %d", code)
|
||||
}
|
||||
// 3. dateOrder 引用非日期属性 → 400(再声明一个 string 属性后测)
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/object-types/"+keyA+"/properties",
|
||||
map[string]any{"api_name": "sp", "display_name": "SP", "data_type": "string"}, h); code != 201 {
|
||||
t.Fatalf("add sp failed")
|
||||
}
|
||||
if code, _ := env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+keyA,
|
||||
map[string]any{"axioms": map[string]any{"dateOrder": []map[string]string{{"before": "p1", "after": "sp"}}}}, h); code != 400 {
|
||||
t.Errorf("dateOrder 非日期属性应 400, got %d", code)
|
||||
}
|
||||
// 4. inverseOf 自指 → 400(声明 {"AtoB":"AtoB"} 同名)
|
||||
if code, _ := env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+keyA,
|
||||
map[string]any{"axioms": map[string]any{"inverseOf": map[string]string{"AtoB": "AtoB"}}}, h); code != 400 {
|
||||
t.Errorf("inverseOf 自指应 400, got %d", code)
|
||||
}
|
||||
// 5. 合法 dateOrder/disjointWith 端→通过
|
||||
if code, _ := env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+keyA,
|
||||
map[string]any{"axioms": map[string]any{"disjointWith": []string{keyB}, "dateOrder": []map[string]string{{"before": "p1", "after": "p2"}}}}, h); code != 200 {
|
||||
t.Errorf("合法公理应 200, got %d", code)
|
||||
}
|
||||
}
|
||||
@@ -101,6 +101,10 @@ func (a *app) createObjectType(c *gin.Context) {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := a.validateAxiomsWellFormed(c, in.APIName, ax); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
axiomsJSON := "{}"
|
||||
if !ax.empty() {
|
||||
axiomsJSON = mustJSON(ax)
|
||||
@@ -174,7 +178,7 @@ func (a *app) updateObjectType(c *gin.Context) {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if !validIRILocalName(in.APIName) {
|
||||
if in.APIName != "" && !validIRILocalName(in.APIName) {
|
||||
c.JSON(400, gin.H{"error": "api_name 不符合 IRI 本地名字符集(首字符字母/下划线,其余字母/数字/_-./~): " + in.APIName})
|
||||
return
|
||||
}
|
||||
@@ -185,19 +189,23 @@ func (a *app) updateObjectType(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
// 公理形状校验(GB/T 48000.3-2026 §8.2):未知键/类型错误一律拒绝,避免拼写错误静默丢失。
|
||||
// update 路径暂以 axiomsUpdate 作为占位写空——任务 3 引入 schema 级良构校验
|
||||
// (类型/链接/属性引用存在性 + inverseOf 互换 + dateOrder 需 date/datetime)后再切到真实流。
|
||||
axiomsRaw := in.Axioms
|
||||
axiomsUpdate := "{}"
|
||||
axiomsPresent := len(axiomsRaw) > 0 && string(axiomsRaw) != "null"
|
||||
if axiomsPresent {
|
||||
_, err := parseAxioms(axiomsRaw)
|
||||
ax, err := parseAxioms(axiomsRaw)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := a.validateAxiomsWellFormed(c, name, ax); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if !ax.empty() {
|
||||
axiomsUpdate = mustJSON(ax)
|
||||
}
|
||||
}
|
||||
_ = axiomsUpdate // 任务 3 接入 validate_axioms 流程后再启用
|
||||
defer invalidateClassCache()
|
||||
// before 快照(更新前,审计用)
|
||||
before := a.objectTypeSnapshot(c, name)
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// validateAxiomsWellFormed 公理声明的 schema 级良构校验。
|
||||
// 拦截点:createObjectType / updateObjectType 写入口,任一不合法 → 400。
|
||||
// 涉及闭包(versionClosure)的规则(父子互斥等)由类型继承任务(任务 7)补全。
|
||||
//
|
||||
// M4:dateOrder 引用的属性只查 self 自身;任务 8(属性继承)接入后升级为闭包。
|
||||
func (a *app) validateAxiomsWellFormed(ctx context.Context, selfName string, ax typeAxioms) error {
|
||||
if ax.empty() {
|
||||
return nil
|
||||
}
|
||||
typeExists := func(n string) bool {
|
||||
var x int
|
||||
return a.pool.QueryRow(ctx, `SELECT 1 FROM object_types WHERE api_name=$1`, n).Scan(&x) == nil
|
||||
}
|
||||
linkOf := func(n string) (apiName, src, tgt string, ok bool) {
|
||||
err := a.pool.QueryRow(ctx,
|
||||
`SELECT api_name, source_type, target_type FROM link_types WHERE api_name=$1`, n).
|
||||
Scan(&apiName, &src, &tgt)
|
||||
return apiName, src, tgt, err == nil
|
||||
}
|
||||
propDataType := func(typeName, prop string) (string, bool) {
|
||||
var dt string
|
||||
err := a.pool.QueryRow(ctx,
|
||||
`SELECT p.data_type FROM properties p JOIN object_types ot ON ot.id=p.object_type_id
|
||||
WHERE ot.api_name=$1 AND p.api_name=$2`, typeName, prop).Scan(&dt)
|
||||
return dt, err == nil
|
||||
}
|
||||
|
||||
for _, n := range ax.DisjointWith {
|
||||
if n == selfName {
|
||||
return fmt.Errorf("axioms.disjointWith: 不能与自身互斥")
|
||||
}
|
||||
if !typeExists(n) {
|
||||
return fmt.Errorf("axioms.disjointWith: 类型 %q 不存在", n)
|
||||
}
|
||||
}
|
||||
for _, n := range ax.EquivalentTo {
|
||||
if n == selfName {
|
||||
return fmt.Errorf("axioms.equivalentTo: 不能与自身等价")
|
||||
}
|
||||
if !typeExists(n) {
|
||||
return fmt.Errorf("axioms.equivalentTo: 类型 %q 不存在", n)
|
||||
}
|
||||
}
|
||||
for _, n := range ax.FunctionalLinks {
|
||||
_, src, _, ok := linkOf(n)
|
||||
if !ok {
|
||||
return fmt.Errorf("axioms.functionalLinks: 链接类型 %q 不存在", n)
|
||||
}
|
||||
if selfName != "" && src != selfName {
|
||||
return fmt.Errorf("axioms.functionalLinks: %q 的 source_type=%q 非本类型", n, src)
|
||||
}
|
||||
}
|
||||
for _, pair := range sortedStringPairs(ax.InverseOf) {
|
||||
a, b := pair[0], pair[1]
|
||||
la, srcA, tgtA, okA := linkOf(a)
|
||||
lb, srcB, tgtB, okB := linkOf(b)
|
||||
if !okA || !okB {
|
||||
return fmt.Errorf("axioms.inverseOf: 链接类型 %q/%q 不存在", a, b)
|
||||
}
|
||||
_ = la
|
||||
_ = lb
|
||||
if srcA != tgtB || tgtA != srcB || a == b {
|
||||
return fmt.Errorf("axioms.inverseOf: %q(%s→%s) 与 %q(%s→%s) 定义域/值域不互换", a, srcA, tgtA, b, srcB, tgtB)
|
||||
}
|
||||
}
|
||||
for _, r := range ax.DateOrder {
|
||||
if r.Before == "" || r.After == "" {
|
||||
return fmt.Errorf("axioms.dateOrder: before/after 属性名必填")
|
||||
}
|
||||
for _, p := range []string{r.Before, r.After} {
|
||||
dt, ok := propDataType(selfName, p)
|
||||
if !ok {
|
||||
return fmt.Errorf("axioms.dateOrder: 属性 %q 未在本类型声明", p)
|
||||
}
|
||||
if dt != "date" && dt != "datetime" {
|
||||
return fmt.Errorf("axioms.dateOrder: 属性 %q data_type=%q 需为 date/datetime", p, dt)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sortedStringPairs 把 map 拆成有序 [k,l] 切片(按 k 排序)——保证错误信息确定性。
|
||||
func sortedStringPairs(m map[string]string) [][2]string {
|
||||
out := make([][2]string, 0, len(m))
|
||||
for k, v := range m {
|
||||
out = append(out, [2]string{k, v})
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i][0] < out[j][0] })
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user