move pk unique and fk out of description and into table properties

This commit is contained in:
2026-09-05 08:40:02 +02:00
parent fe83013c37
commit 1225ff2a30
3 changed files with 292 additions and 190 deletions
+185 -113
View File
@@ -155,7 +155,7 @@ spec withConfig = withConfig baseCfg $ describe "OpenAPI" $ do
"description": "child_entities comment",
"properties": {
"id": {
"description": "child_entities id comment\n\nNote:\nThis is a Primary Key.<pk/>",
"description": "child_entities id comment",
"format": "int32",
"type": "integer"
},
@@ -165,13 +165,29 @@ spec withConfig = withConfig baseCfg $ describe "OpenAPI" $ do
"type": "string"
},
"parent_id": {
"description": "Note:\nThis is a Foreign Key to `entities.id`.<fk table='entities' columns='parent_id:id'/>",
"format": "int32",
"type": "integer"
}
},
"required": [
"id"
],
"x-primary-key": [
"id"
],
"x-unique": [],
"x-foreign-keys": [
{
"columns": [
"parent_id"
],
"references": {
"table": "entities",
"columns": [
"id"
]
}
}
]
}
|]
@@ -190,7 +206,7 @@ spec withConfig = withConfig baseCfg $ describe "OpenAPI" $ do
"description": "child_entities_view comment",
"properties": {
"id": {
"description": "child_entities_view id comment\n\nNote:\nThis is a Primary Key.<pk/>",
"description": "child_entities_view id comment",
"format": "int32",
"type": "integer"
},
@@ -200,11 +216,27 @@ spec withConfig = withConfig baseCfg $ describe "OpenAPI" $ do
"type": "string"
},
"parent_id": {
"description": "Note:\nThis is a Foreign Key to `entities.id`.<fk table='entities' columns='parent_id:id'/>",
"format": "int32",
"type": "integer"
}
}
},
"x-primary-key": [
"id"
],
"x-unique": [],
"x-foreign-keys": [
{
"columns": [
"parent_id"
],
"references": {
"table": "entities",
"columns": [
"id"
]
}
}
]
}
|]
@@ -274,122 +306,146 @@ spec withConfig = withConfig baseCfg $ describe "OpenAPI" $ do
filterEmail `shouldNotBe` Nothing
filterPassword `shouldBe` Nothing
it "includes a fk description for a O2O relationship" $ do
it "includes foreign key and unique metadata for a O2O relationship" $ do
r <- simpleBody <$> get "/"
let referralLink = r ^? key "definitions" . key "first" . key "properties" . key "second_id_1"
let firstFks = r ^? key "definitions" . key "first" . key "x-foreign-keys"
firstUniques = r ^? key "definitions" . key "first" . key "x-unique"
liftIO $
referralLink `shouldBe` Just
liftIO $ do
firstFks `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is a Unique column.<unique/>\nThis is a Foreign Key to `second.id`.<fk table='second' columns='second_id_1:id'/>"
}
[
{
"columns": [
"second_id_1"
],
"references": {
"table": "second",
"columns": [
"id"
]
}
},
{
"columns": [
"second_id_2"
],
"references": {
"table": "second",
"columns": [
"id"
]
}
}
]
|]
firstUniques `shouldBe` Just
[aesonQQ|[["second_id_1"], ["second_id_2"]]|]
it "includes a unique description for a column with a unique constraint" $ do
it "includes a unique column in the unique metadata" $ do
r <- simpleBody <$> get "/"
let uniqueKey = r ^? key "definitions" . key "single_unique" . key "properties" . key "unique_key"
let uniqueCols = r ^? key "definitions" . key "single_unique" . key "x-unique"
liftIO $
uniqueKey `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is a Unique column.<unique/>"
}
|]
uniqueCols `shouldBe` Just
[aesonQQ|[["unique_key"]]|]
it "includes the column list of a composite unique constraint" $ do
r <- simpleBody <$> get "/"
let compoundKey1 = r ^? key "definitions" . key "compound_unique" . key "properties" . key "key1"
compoundKey2 = r ^? key "definitions" . key "compound_unique" . key "properties" . key "key2"
let uniqueCols = r ^? key "definitions" . key "compound_unique" . key "x-unique"
liftIO $ do
compoundKey1 `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is part of a composite unique constraint.<unique cols='key1,key2'/>"
}
|]
compoundKey2 `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is part of a composite unique constraint.<unique cols='key1,key2'/>"
}
|]
liftIO $
uniqueCols `shouldBe` Just
[aesonQQ|[["key1", "key2"]]|]
it "includes the column list for mixed single and composite unique constraints" $ do
r <- simpleBody <$> get "/"
let uniqueCol = r ^? key "definitions" . key "mixed_unique" . key "properties" . key "id"
compoundKey1 = r ^? key "definitions" . key "mixed_unique" . key "properties" . key "key1"
compoundKey2 = r ^? key "definitions" . key "mixed_unique" . key "properties" . key "key2"
let uniqueCols = r ^? key "definitions" . key "mixed_unique" . key "x-unique"
liftIO $
uniqueCols `shouldBe` Just
[aesonQQ|[["id"], ["key1", "key2"]]|]
it "includes composite foreign key metadata with the full column mapping" $ do
r <- simpleBody <$> get "/"
let fks = r ^? key "definitions" . key "comp_component_instance" . key "x-foreign-keys"
liftIO $
fks `shouldBe` Just
[aesonQQ|
[
{
"columns": [
"product_id",
"component_id"
],
"references": {
"table": "comp_component",
"columns": [
"product_id",
"component_id"
]
}
},
{
"columns": [
"component_instance_id",
"component_id"
],
"references": {
"table": "comp_product_instance",
"columns": [
"id",
"product_id"
]
}
},
{
"columns": [
"product_instance_id",
"product_id"
],
"references": {
"table": "comp_product_instance",
"columns": [
"id",
"product_id"
]
}
}
]
|]
it "includes single column foreign key metadata with the unified format" $ do
r <- simpleBody <$> get "/"
let fks = r ^? key "definitions" . key "comp_product_instance" . key "x-foreign-keys"
uniques = r ^? key "definitions" . key "comp_product_instance" . key "x-unique"
liftIO $ do
uniqueCol `shouldBe` Just
fks `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is a Unique column.<unique/>"
}
|]
compoundKey1 `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is part of a composite unique constraint.<unique cols='key1,key2'/>"
}
|]
compoundKey2 `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is part of a composite unique constraint.<unique cols='key1,key2'/>"
}
|]
it "includes a composite foreign key marker with the full column mapping" $ do
r <- simpleBody <$> get "/"
let parentFk = r ^? key "definitions" . key "comp_component_instance" . key "properties" . key "product_instance_id"
liftIO $
parentFk `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is a Primary Key.<pk/>\nThis is a Foreign Key to `comp_product_instance.(id, product_id)`.<fk table='comp_product_instance' columns='product_instance_id:id,product_id:product_id'/>"
}
|]
it "includes a single column foreign key marker with the unified format" $ do
r <- simpleBody <$> get "/"
let productFk = r ^? key "definitions" . key "comp_product_instance" . key "properties" . key "product_id"
liftIO $
productFk `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is part of a composite unique constraint.<unique cols='id,product_id'/>\nThis is a Foreign Key to `comp_product.id`.<fk table='comp_product' columns='product_id:id'/>"
}
[
{
"columns": [
"product_id"
],
"references": {
"table": "comp_product",
"columns": [
"id"
]
}
}
]
|]
uniques `shouldBe` Just
[aesonQQ|[["id", "product_id"]]|]
describe "Foreign table" $
@@ -481,19 +537,27 @@ spec withConfig = withConfig baseCfg $ describe "OpenAPI" $ do
describe "VIEW that has a source FK based on a UNIQUE key" $
it "includes fk description" $ do
it "includes fk metadata" $ do
r <- simpleBody <$> get "/"
let referralLink = r ^? key "definitions" . key "referrals" . key "properties" . key "link"
let fks = r ^? key "definitions" . key "referrals" . key "x-foreign-keys"
liftIO $
referralLink `shouldBe` Just
fks `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is a Foreign Key to `pages.link`.<fk table='pages' columns='link:link'/>"
}
[
{
"columns": [
"link"
],
"references": {
"table": "pages",
"columns": [
"link"
]
}
}
]
|]
describe "VIEW created for a TABLE with a O2M relationship" $ do
@@ -501,16 +565,24 @@ spec withConfig = withConfig baseCfg $ describe "OpenAPI" $ do
it "fk points to destination TABLE instead of the VIEW" $ do
r <- simpleBody <$> get "/"
let referralLink = r ^? key "definitions" . key "projects" . key "properties" . key "client_id"
let fks = r ^? key "definitions" . key "projects" . key "x-foreign-keys"
liftIO $
referralLink `shouldBe` Just
fks `shouldBe` Just
[aesonQQ|
{
"format": "int32",
"type": "integer",
"description": "Note:\nThis is a Foreign Key to `clients.id`.<fk table='clients' columns='client_id:id'/>"
}
[
{
"columns": [
"client_id"
],
"references": {
"table": "clients",
"columns": [
"id"
]
}
}
]
|]
describe "PostgreSQL to Swagger Type Mapping" $ do
+24 -8
View File
@@ -282,7 +282,6 @@ spec withConfig = withConfig (baseCfg { configDbSchemas = fromList ["v1", "v2",
"type" : "object",
"properties" : {
"id" : {
"description" : "Note:\nThis is a Primary Key.<pk/>",
"format" : "int32",
"type" : "integer"
},
@@ -293,7 +292,12 @@ spec withConfig = withConfig (baseCfg { configDbSchemas = fromList ["v1", "v2",
},
"required" : [
"id"
]
],
"x-foreign-keys" : [],
"x-primary-key" : [
"id"
],
"x-unique" : []
}
|]
@@ -311,7 +315,6 @@ spec withConfig = withConfig (baseCfg { configDbSchemas = fromList ["v1", "v2",
"type" : "object",
"properties" : {
"id" : {
"description" : "Note:\nThis is a Primary Key.<pk/>",
"format" : "int32",
"type" : "integer"
},
@@ -322,7 +325,12 @@ spec withConfig = withConfig (baseCfg { configDbSchemas = fromList ["v1", "v2",
},
"required" : [
"id"
]
],
"x-foreign-keys" : [],
"x-primary-key" : [
"id"
],
"x-unique" : []
}
|]
@@ -340,7 +348,6 @@ spec withConfig = withConfig (baseCfg { configDbSchemas = fromList ["v1", "v2",
"type" : "object",
"properties" : {
"id" : {
"description" : "Note:\nThis is a Primary Key.<pk/>",
"format" : "int32",
"type" : "integer"
},
@@ -351,7 +358,12 @@ spec withConfig = withConfig (baseCfg { configDbSchemas = fromList ["v1", "v2",
},
"required" : [
"id"
]
],
"x-foreign-keys" : [],
"x-primary-key" : [
"id"
],
"x-unique" : []
}
|]
@@ -369,7 +381,6 @@ spec withConfig = withConfig (baseCfg { configDbSchemas = fromList ["v1", "v2",
"type" : "object",
"properties" : {
"id" : {
"description" : "Note:\nThis is a Primary Key.<pk/>",
"format" : "int32",
"type" : "integer"
},
@@ -380,7 +391,12 @@ spec withConfig = withConfig (baseCfg { configDbSchemas = fromList ["v1", "v2",
},
"required" : [
"id"
]
],
"x-foreign-keys" : [],
"x-primary-key" : [
"id"
],
"x-unique" : []
}
|]