Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W-11460908: remove full JSON path from property terms in JSON-LD objects #1800

Merged
merged 1 commit into from
May 10, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ case class JsonLDObjectElementParser(
override def findClassTerm(ctx: SemanticContext): Seq[String] = {
val terms = super.findClassTerm(ctx).map(ctx.expand)
if (terms.isEmpty) {
val fragment = if (path.toString.isEmpty) key else path.toString
val fragment = lastNameFromPath() match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole if-clause could be refactored into a method so we isolate @id computation logic

case Some(name) => name
case None => key
}
Seq(ctx.expand(ctx.computeBase(fragment)))
} else terms
}

def lastNameFromPath(): Option[String] = {
val numberRegex = "\\d+\\.?\\d*"
path.segments.reverse.find { segment => !segment.matches(numberRegex) }
}

private def parseWithObject(n: NodeShape): JsonLDObjectElementBuilder = parseDynamic(n.properties, n.semanticContext)

private def parseDynamic(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,16 @@ case class JsonLDPropertyParser(
ctx.eh.violation(ContainerCheckErrorList, range, ContainerCheckErrorList.message)

private def computeTerm(path: JsonPath): String = {
propertyName(path)
.flatMap(semanticContext.getTermFor)
.map(semanticContext.expand)
.getOrElse(semanticContext.computeBase(path.toString))
propertyName(path) match {
case Some(name) =>
semanticContext
.getTermFor(name)
.map(semanticContext.expand)
.getOrElse(semanticContext.computeBase(name))

// should be unreachable
case _ => throw new IllegalStateException(s"Property ${path.toString} does not have name")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Out of Scope] - This is shady, we should have a safe-compiler way of doing this check (I don't know how at the moment)

}
}

private def propertyName(path: JsonPath): Option[String] = path.lastSegment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
"anypoint://vocabulary/security.yaml#inner1",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"amlorg://a.ml/semantics.yaml#prop1/inner1": {
"amlorg://a.ml/semantics.yaml#inner1": {
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/cycle-jsonld/uri-expansion/class-term-complex/instance.json#/encodes/prop1/inner1"
}
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/cycle-jsonld/uri-expansion/class-term-complex/instance.json#/encodes/prop1/inner1",
"@type": [
"amlorg://a.ml/semantics.yaml#prop1/inner1",
"amlorg://a.ml/semantics.yaml#inner1",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"amlorg://a.ml/semantics.yaml#prop1/inner1/a1": "asdfasf"
"amlorg://a.ml/semantics.yaml#a1": "asdfasf"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/cycle-jsonld/uri-expansion/class-term-complex/instance.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"anypoint://vocabulary/security.yaml#sensitive",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"amlorg://a.ml/semantics.yaml#prop1/inner1": "asdfasf"
"amlorg://a.ml/semantics.yaml#inner1": "asdfasf"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/cycle-jsonld/uri-expansion/class-term-nested/instance.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"anypoint://vocabulary/policy.yaml#sensitive",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"amlorg://a.ml/semantics.yaml#prop1/inner1": "asdfasf"
"amlorg://a.ml/semantics.yaml#inner1": "asdfasf"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/cycle-jsonld/uri-expansion/class-term-simple/instance.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"http://a.ml/vocabularies/core#metadata",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#metadata/name": "products-users-api",
"http://a.ml/vocabularies/core#metadata/labels": {
"http://a.ml/vocabularies/core#name": "products-users-api",
"http://a.ml/vocabularies/core#labels": {
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/metadata/labels"
}
},
Expand All @@ -39,12 +39,12 @@
"http://a.ml/vocabularies/core#spec",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#spec/address": "proxyUri",
"http://a.ml/vocabularies/core#spec/deprecated": false,
"http://a.ml/vocabularies/core#spec/deployment": {
"http://a.ml/vocabularies/core#address": "proxyUri",
"http://a.ml/vocabularies/core#deprecated": false,
"http://a.ml/vocabularies/core#deployment": {
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/spec/deployment"
},
"http://a.ml/vocabularies/core#spec/policies": [
"http://a.ml/vocabularies/core#policies": [
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/spec/policies/0"
},
Expand All @@ -56,58 +56,58 @@
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/metadata/labels",
"@type": [
"http://a.ml/vocabularies/core#metadata/labels",
"http://a.ml/vocabularies/core#labels",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#metadata/labels/flex.mulesoft.com/api-group-id": "1caa0b9b-4f4d-43c4-a1b6-f925a8c77baa",
"http://a.ml/vocabularies/core#metadata/labels/flex.mulesoft.com/api-asset-id": "assetId",
"http://a.ml/vocabularies/core#metadata/labels/flex.mulesoft.com/api-version": "1.0.0"
"http://a.ml/vocabularies/core#flex.mulesoft.com/api-group-id": "1caa0b9b-4f4d-43c4-a1b6-f925a8c77baa",
"http://a.ml/vocabularies/core#flex.mulesoft.com/api-asset-id": "assetId",
"http://a.ml/vocabularies/core#flex.mulesoft.com/api-version": "1.0.0"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/spec/deployment",
"@type": [
"http://a.ml/vocabularies/core#spec/deployment",
"http://a.ml/vocabularies/core#deployment",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#spec/deployment/target": "CH",
"http://a.ml/vocabularies/core#spec/deployment/runtimeVersion": "4.4.0",
"http://a.ml/vocabularies/core#spec/deployment/technology": "FlexGateway"
"http://a.ml/vocabularies/core#target": "CH",
"http://a.ml/vocabularies/core#runtimeVersion": "4.4.0",
"http://a.ml/vocabularies/core#technology": "FlexGateway"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/spec/policies/0",
"@type": [
"http://a.ml/vocabularies/core#spec/policies/0",
"http://a.ml/vocabularies/core#policies",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#spec/policies/0/policyRef": {
"http://a.ml/vocabularies/core#policyRef": {
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/spec/policies/0/policyRef"
}
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/spec/policies/1",
"@type": [
"http://a.ml/vocabularies/core#spec/policies/1",
"http://a.ml/vocabularies/core#policies",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#spec/policies/1/policyRef": {
"http://a.ml/vocabularies/core#policyRef": {
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/spec/policies/1/policyRef"
}
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/spec/policies/0/policyRef",
"@type": [
"http://a.ml/vocabularies/core#spec/policies/0/policyRef",
"http://a.ml/vocabularies/core#policyRef",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#spec/policies/0/policyRef/name": "http-basic-authentication-flex"
"http://a.ml/vocabularies/core#name": "http-basic-authentication-flex"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml#/encodes/spec/policies/1/policyRef",
"@type": [
"http://a.ml/vocabularies/core#spec/policies/1/policyRef",
"http://a.ml/vocabularies/core#policyRef",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#spec/policies/1/policyRef/name": "rate-limiting-flex"
"http://a.ml/vocabularies/core#name": "rate-limiting-flex"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/gcl/instance1.yaml",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/model/compacted-array-values.json#/encodes/anyArray2/0",
"@type": [
"http://a.ml/vocabularies/core#anyArray2/0",
"http://a.ml/vocabularies/core#anyArray2",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#anyArray2/0/something": "a"
"http://a.ml/vocabularies/core#something": "a"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/model/compacted-array-values.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"amlorg://a.ml/semantics.yaml#itemObject",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"amlorg://a.ml/semantics.yaml#0/a": "value"
"amlorg://a.ml/semantics.yaml#a": "value"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/semantics/array/6.1.1-c.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/semantics/class-term-addition-in-inner-property.json#/encodes/d/0",
"@type": [
"http://prefix.com#D",
"http://a.ml/vocabularies/core#d/0",
"http://a.ml/vocabularies/core#d",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://prefix.com#e": "something"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"http://a.ml/vocabularies/core#prop1",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#prop1/innerProp": "value"
"http://a.ml/vocabularies/core#innerProp": "value"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/semantics/object/6.1.1-c.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"amlorg://a.ml/semantics.yaml#prop1",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"amlorg://a.ml/semantics.yaml#prop1/prop2": "something"
"amlorg://a.ml/semantics.yaml#prop2": "something"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/semantics/pattern-property-should-keep-json-prop-name.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
"http://a.ml/vocabularies/core#c",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#c/d": "d property",
"http://a.ml/vocabularies/core#d": "d property",
"http://a.ml/vocabularies/aml#e": {
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/semantics/propagation/nested-semantics-redefinition.json#/encodes/c/e"
}
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/semantics/propagation/nested-semantics-redefinition.json#/encodes/c/e",
"@type": [
"http://a.ml/vocabularies/core#c/e",
"http://a.ml/vocabularies/core#e",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/aml#f": "f-property"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
"http://a.ml/vocabularies/core#a",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#a/b": {
"http://a.ml/vocabularies/core#b": {
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/semantics/propagation/type-mappings-shouldnt-propagate.json#/encodes/a/b"
}
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/semantics/propagation/type-mappings-shouldnt-propagate.json#/encodes/a/b",
"@type": [
"http://a.ml/vocabularies/core#a/b",
"http://a.ml/vocabularies/core#b",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#a/b/c": "something"
"http://a.ml/vocabularies/core#c": "something"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/semantics/propagation/type-mappings-shouldnt-propagate.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"http://a.ml/vocabularies/core#0",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#0/prop1": "value1"
"http://a.ml/vocabularies/core#prop1": "value1"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/simple/array/6.1.1-c.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"http://a.ml/vocabularies/core#0",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#0/name": "Roman"
"http://a.ml/vocabularies/core#name": "Roman"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/simple/array/core-9.3.2.1.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"http://a.ml/vocabularies/core#prop1",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#prop1/propInner": "value1"
"http://a.ml/vocabularies/core#propInner": "value1"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/simple/object/6.1.1-c.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"http://a.ml/vocabularies/core#prop1",
"http://a.ml/vocabularies/document#JsonLDObject"
],
"http://a.ml/vocabularies/core#prop1/name": "roman"
"http://a.ml/vocabularies/core#name": "roman"
},
{
"@id": "file://amf-shapes/shared/src/test/resources/jsonld-schema/instances/simple/object/core-9.3.2.1.json",
Expand Down