Skip to content

Commit

Permalink
fix: Migrate resource metadata from contentStream to contentMetadata …
Browse files Browse the repository at this point in the history
…[DEV-4767] (#348)

* fix: Fix resource & resource/metadata endpoints

* gofumpt

* Handle default browser header

* feat: Migrate metadata list from contentStream to contentMetadata

* Fix unit tests

* Add unmarshaller for ResourceDereferencing && isBrowser

* Remove IsBrowser fix

---------

Co-authored-by: Ankur Banerjee <[email protected]>
  • Loading branch information
DaevMithran and ankurdotb authored Feb 25, 2025
1 parent 7087e0b commit 43c05d7
Show file tree
Hide file tree
Showing 25 changed files with 441 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (d *ResourceMetadataHandler) Handle(c services.ResolverContext, service ser
}

if resourceMetadata == "true" {
dereferencingResult := types.NewResourceDereferencingFromContent(
service.GetDid(), service.GetContentType(), &types.ResolutionDidDocMetadata{Resources: didResolution.Metadata.Resources},
dereferencingResult := types.NewResourceDereferencingFromResources(
service.GetDid(), service.GetContentType(), &didResolution.Metadata.Resources,
)
return d.Continue(c, service, dereferencingResult)
}
Expand Down
3 changes: 1 addition & 2 deletions services/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ func GetPriorityContentType(acceptHeader string, resource bool) (types.ContentTy
}
for _, at := range acceptedTypes {
mediaType := types.ContentType(at.Type + "/" + at.Subtype)

// If the Header contains any media type, return the default content type
if mediaType == "*/*" {
if mediaType == types.DEFAULT_RESOLUTION_TYPE {
if resource { // If request is from Resource Handlers
return types.JSONLD, ""
} else { // If request is from DIDDoc Handlers
Expand Down
5 changes: 3 additions & 2 deletions services/request_service_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func (dd *BaseRequestService) BasicPrepare(c ResolverContext) error {
isDereferencingOrFragment := dd.IsDereferencing && dd.Fragment == ""
// Get Accept header
dd.RequestedContentType, _ = GetPriorityContentType(c.Request().Header.Get(echo.HeaderAccept), isDereferencingOrFragment)
if !dd.GetContentType().IsSupported() {
return types.NewRepresentationNotSupportedError(dd.GetDid(), dd.GetContentType(), nil, dd.IsDereferencing)
// Ignore supported type for dereferencing
if !isDereferencingOrFragment && !dd.GetContentType().IsSupported() {
return types.NewRepresentationNotSupportedError(dd.GetDid(), types.JSON, nil, dd.IsDereferencing)
}

// Get DID from request
Expand Down
5 changes: 5 additions & 0 deletions services/resource/resource_metadata_dereferencing.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func (dr ResourceMetadataDereferencingService) Redirect(c services.ResolverConte
}

func (dr *ResourceMetadataDereferencingService) SpecificValidation(c services.ResolverContext) error {
// Metadata endpoint should be one of the supported types.
if !dr.RequestedContentType.IsSupported() {
return types.NewRepresentationNotSupportedError(dr.GetDid(), types.JSON, nil, dr.IsDereferencing)
}

if !utils.IsValidUUID(dr.ResourceId) {
return types.NewInvalidDidUrlError(dr.ResourceId, dr.RequestedContentType, nil, dr.IsDereferencing)
}
Expand Down
4 changes: 2 additions & 2 deletions services/resource_dereference_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (rds ResourceService) DereferenceResourceMetadata(did string, resourceId st
}

metadata := types.NewDereferencedResource(did, resource.Metadata)
return &types.ResourceDereferencing{Context: context, Metadata: metadata, DereferencingMetadata: dereferenceMetadata}, nil
return &types.ResourceDereferencing{Context: context, Metadata: &types.ResolutionResourceMetadata{ContentMetadata: metadata}, DereferencingMetadata: dereferenceMetadata}, nil
}

func (rds ResourceService) ResolveMetadataResources(did string, contentType types.ContentType) (*types.DidResolution, *types.IdentityError) {
Expand Down Expand Up @@ -125,5 +125,5 @@ func (rds ResourceService) DereferenceResourceDataWithMetadata(did string, resou
}
}

return &types.ResourceDereferencing{Context: context, ContentStream: result, Metadata: metadata, DereferencingMetadata: dereferenceMetadata}, nil
return &types.ResourceDereferencing{Context: context, ContentStream: result, Metadata: &types.ResolutionResourceMetadata{ContentMetadata: metadata}, DereferencingMetadata: dereferenceMetadata}, nil
}
3 changes: 2 additions & 1 deletion tests/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var (
IndyStyleTestnetDid = "did:cheqd:testnet:73wnEyHhkhXiH1Nq7w5Kgq"

UUIDStyleTestnetId = "c1685ca0-1f5b-439c-8eb8-5c0e85ab7cd0"
UUIDStyleMainnetDid = "did:cheqd:mainnet:c82f2b02-bdab-4dd7-b833-3e143745d612"
UUIDStyleMainnetId = "c82f2b02-bdab-4dd7-b833-3e143745d612"
UUIDStyleMainnetDid = "did:cheqd:mainnet:" + UUIDStyleMainnetId
UUIDStyleTestnetDid = "did:cheqd:testnet:" + UUIDStyleTestnetId
UUIDTestnetDidIdForImage = "did:cheqd:testnet:55dbc8bf-fba3-4117-855c-1e0dc1d3bb47"

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/rest/diddoc/metadata/negative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var _ = DescribeTable("Negative: Get DIDDoc version metadata", func(testCase uti
ExpectedResult: types.DidResolution{
Context: "",
ResolutionMetadata: types.ResolutionMetadata{
ContentType: types.TEXT,
ContentType: types.JSON,
ResolutionError: "representationNotSupported",
DidProperties: types.DidProperties{},
},
Expand All @@ -68,7 +68,7 @@ var _ = DescribeTable("Negative: Get DIDDoc version metadata", func(testCase uti
ExpectedResult: types.DidResolution{
Context: "",
ResolutionMetadata: types.ResolutionMetadata{
ContentType: types.TEXT,
ContentType: types.JSON,
ResolutionError: "representationNotSupported",
DidProperties: types.DidProperties{},
},
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/rest/diddoc/version/negative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var _ = DescribeTable("Negative: Get DIDDoc version", func(testCase utils.Negati
ExpectedResult: types.DidResolution{
Context: "",
ResolutionMetadata: types.ResolutionMetadata{
ContentType: types.TEXT,
ContentType: types.JSON,
ResolutionError: "representationNotSupported",
DidProperties: types.DidProperties{},
},
Expand All @@ -68,7 +68,7 @@ var _ = DescribeTable("Negative: Get DIDDoc version", func(testCase utils.Negati
ExpectedResult: types.DidResolution{
Context: "",
ResolutionMetadata: types.ResolutionMetadata{
ContentType: types.TEXT,
ContentType: types.JSON,
ResolutionError: "representationNotSupported",
DidProperties: types.DidProperties{},
},
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/rest/diddoc/versions/negative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var _ = DescribeTable("Negative: Get DIDDoc versions", func(testCase utils.Negat
ExpectedResult: types.DidResolution{
Context: "",
ResolutionMetadata: types.ResolutionMetadata{
ContentType: types.TEXT,
ContentType: types.JSON,
ResolutionError: "representationNotSupported",
DidProperties: types.DidProperties{},
},
Expand All @@ -66,7 +66,7 @@ var _ = DescribeTable("Negative: Get DIDDoc versions", func(testCase utils.Negat
ExpectedResult: types.DidResolution{
Context: "",
ResolutionMetadata: types.ResolutionMetadata{
ContentType: types.TEXT,
ContentType: types.JSON,
ResolutionError: "representationNotSupported",
DidProperties: types.DidProperties{},
},
Expand Down
37 changes: 8 additions & 29 deletions tests/integration/rest/resource/data/negative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,8 @@ var _ = DescribeTable("Negative: Get resource data", func(testCase utils.Negativ
expectedDidDereferencing := testCase.ExpectedResult.(utils.DereferencingResult)
utils.AssertDidDereferencing(expectedDidDereferencing, receivedDidDereferencing)
},

Entry(
"cannot get resource data with an existent DID, but not supported ResolutionType",
utils.NegativeTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s",
testconstants.TestHostAddress,
testconstants.UUIDStyleMainnetDid,
testconstants.ValidIdentifier,
),
ResolutionType: string(types.TEXT),
ExpectedResult: utils.DereferencingResult{
Context: "",
DereferencingMetadata: types.DereferencingMetadata{
ContentType: types.TEXT,
ResolutionError: "representationNotSupported",
DidProperties: types.DidProperties{},
},
ContentStream: nil,
Metadata: types.ResolutionDidDocMetadata{},
},
ExpectedStatusCode: errors.RepresentationNotSupportedHttpCode,
},
),

Entry(
"cannot get resource data with not existent DID and not supported ResolutionType",
"cannot get resource data with not existent DID",
utils.NegativeTestCase{
DidURL: fmt.Sprintf(
"http://%s/1.0/identifiers/%s/resources/%s",
Expand All @@ -69,13 +44,17 @@ var _ = DescribeTable("Negative: Get resource data", func(testCase utils.Negativ
Context: "",
DereferencingMetadata: types.DereferencingMetadata{
ContentType: types.TEXT,
ResolutionError: "representationNotSupported",
DidProperties: types.DidProperties{},
ResolutionError: "notFound",
DidProperties: types.DidProperties{
DidString: testconstants.NotExistentMainnetDid,
MethodSpecificId: testconstants.NotExistentIdentifier,
Method: testconstants.ValidMethod,
},
},
ContentStream: nil,
Metadata: types.ResolutionDidDocMetadata{},
},
ExpectedStatusCode: errors.RepresentationNotSupportedHttpCode,
ExpectedStatusCode: errors.NotFoundHttpCode,
},
),

Expand Down
16 changes: 12 additions & 4 deletions tests/integration/rest/resource/metadata/negative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ var _ = DescribeTable("Negative: get resource metadata", func(testCase utils.Neg
ExpectedResult: utils.ResourceDereferencingResult{
Context: "",
DereferencingMetadata: types.DereferencingMetadata{
ContentType: types.TEXT,
ContentType: types.JSON,
ResolutionError: "representationNotSupported",
DidProperties: types.DidProperties{},
DidProperties: types.DidProperties{
DidString: testconstants.UUIDStyleMainnetDid,
MethodSpecificId: testconstants.UUIDStyleMainnetId,
Method: testconstants.ValidMethod,
},
},
ContentStream: nil,
Metadata: &types.DereferencedResource{},
Expand All @@ -68,9 +72,13 @@ var _ = DescribeTable("Negative: get resource metadata", func(testCase utils.Neg
ExpectedResult: utils.ResourceDereferencingResult{
Context: "",
DereferencingMetadata: types.DereferencingMetadata{
ContentType: types.TEXT,
ContentType: types.JSON,
ResolutionError: "representationNotSupported",
DidProperties: types.DidProperties{},
DidProperties: types.DidProperties{
DidString: testconstants.NotExistentMainnetDid,
MethodSpecificId: testconstants.NotExistentIdentifier,
Method: testconstants.ValidMethod,
},
},
ContentStream: nil,
Metadata: &types.DereferencedResource{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"method": "cheqd"
}
},
"contentStream": {
"contentMetadata": {
"linkedResourceMetadata": [
{
"resourceURI": "did:cheqd:testnet:c1685ca0-1f5b-439c-8eb8-5c0e85ab7cd0/resources/9ba3922e-d5f5-4f53-b265-fc0d4e988c77",
Expand Down Expand Up @@ -39,5 +39,5 @@
}
]
},
"contentMetadata": null
"contentStream": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"method": "cheqd"
}
},
"contentStream": {
"contentMetadata": {
"linkedResourceMetadata": [
{
"resourceURI": "did:cheqd:testnet:c1685ca0-1f5b-439c-8eb8-5c0e85ab7cd0/resources/9ba3922e-d5f5-4f53-b265-fc0d4e988c77",
Expand Down Expand Up @@ -39,5 +39,5 @@
}
]
},
"contentMetadata": {}
"contentStream": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"method": "cheqd"
}
},
"contentStream": {
"contentMetadata": {
"linkedResourceMetadata": [
{
"resourceURI": "did:cheqd:testnet:3KpiDD6Hxs4i2G7FtpiGhu/resources/f31c68e6-61b5-4926-a932-40a13b4c4507",
Expand All @@ -26,5 +26,5 @@
}
]
},
"contentMetadata": {}
"contentStream": null
}
Loading

0 comments on commit 43c05d7

Please sign in to comment.