Skip to content

Commit 197a4ba

Browse files
ddey2lmarini
andauthored
Enabling @context in metadata and metadata definition (#750)
* Enabling @context in metadata and metadata definition * adding test * adding codegen files * removing redundant code block * Fixed typo. --------- Co-authored-by: Luigi Marini <[email protected]>
1 parent 2774034 commit 197a4ba

File tree

7 files changed

+27
-9
lines changed

7 files changed

+27
-9
lines changed

backend/app/models/metadata.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import datetime
33
from typing import Optional, List, Union
44

5-
from beanie import Document, PydanticObjectId
5+
from beanie import Document
66
from elasticsearch import Elasticsearch, NotFoundError
77
from fastapi import HTTPException
88
from pydantic import Field, validator, AnyUrl, BaseModel
@@ -11,7 +11,6 @@
1111
EventListenerIn,
1212
LegacyEventListenerIn,
1313
EventListenerOut,
14-
ExtractorInfo,
1514
)
1615
from app.models.mongomodel import MongoDBRef
1716
from app.models.users import UserOut
@@ -101,6 +100,14 @@ class MetadataDefinitionBase(BaseModel):
101100
class Settings:
102101
name = "metadata_definitions"
103102

103+
class Config:
104+
# Serialization Config Options
105+
# Specify JSON key names
106+
# This will rename the field `context` to `@context` in the JSON output
107+
fields = {"context": {"alias": "@context"}}
108+
# This will allow input by field name 'context' too along with '@context'
109+
allow_population_by_field_name = True
110+
104111

105112
class MetadataDefinitionIn(MetadataDefinitionBase):
106113
pass
@@ -184,6 +191,14 @@ class MetadataBase(BaseModel):
184191
str
185192
] # This will be fetched from metadata definition if one is provided (shown by GUI)
186193

194+
class Config:
195+
# Serialization Config Options
196+
# Specify JSON key names
197+
# This will rename the field `context` to `@context` in the JSON output
198+
fields = {"context": {"alias": "@context"}}
199+
# This will allow input by field name 'context' too along with '@context'
200+
allow_population_by_field_name = True
201+
187202
@validator("context")
188203
def contexts_are_valid(cls, v):
189204
if False:

backend/app/tests/test_metadata.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
metadata_definition = {
1111
"name": "LatLon",
1212
"description": "A set of Latitude/Longitude coordinates",
13-
"context": [
13+
"@context": [
1414
{
1515
"longitude": "https://schema.org/longitude",
1616
"latitude": "https://schema.org/latitude",
@@ -37,7 +37,7 @@
3737
metadata_definition2 = {
3838
"name": "AlternativeTitle",
3939
"description": "Alternative title",
40-
"context": [{"title": "https://schema.org/alternateName"}],
40+
"@context": [{"title": "https://schema.org/alternateName"}],
4141
"fields": [
4242
{
4343
"name": "alternateName",
@@ -85,6 +85,9 @@ def test_dataset_create_metadata_definition(client: TestClient, headers: dict):
8585
response.status_code == 200 or response.status_code == 409
8686
) # 409 = definition already exists
8787

88+
# check if @context is injected correctly
89+
assert response.json().get("@context") is not None
90+
8891
# Create dataset and add metadata to it using new definition
8992
dataset_id = create_dataset(client, headers).get("id")
9093
response = client.post(

frontend/src/openapi/v2/models/MetadataDefinitionIn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import type { MetadataField } from './MetadataField';
4242
export type MetadataDefinitionIn = {
4343
name: string;
4444
description?: string;
45-
context?: Array<string>;
45+
'@context'?: Array<string>;
4646
context_url?: string;
4747
fields: Array<MetadataField>;
4848
}

frontend/src/openapi/v2/models/MetadataDefinitionOut.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type { UserOut } from './UserOut';
2121
export type MetadataDefinitionOut = {
2222
name: string;
2323
description?: string;
24-
context?: Array<string>;
24+
'@context'?: Array<string>;
2525
context_url?: string;
2626
fields: Array<MetadataField>;
2727
_id?: string;

frontend/src/openapi/v2/models/MetadataIn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { EventListenerIn } from './EventListenerIn';
66
import type { LegacyEventListenerIn } from './LegacyEventListenerIn';
77

88
export type MetadataIn = {
9-
context?: Array<string>;
9+
'@context'?: Array<string>;
1010
context_url?: string;
1111
definition?: string;
1212
content: any;

frontend/src/openapi/v2/models/MetadataOut.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { MongoDBRef } from './MongoDBRef';
1919
* - [UpdateMethods](https://roman-right.github.io/beanie/api/interfaces/#aggregatemethods)
2020
*/
2121
export type MetadataOut = {
22-
context?: Array<string>;
22+
'@context'?: Array<string>;
2323
context_url?: string;
2424
definition?: string;
2525
content: any;

frontend/src/openapi/v2/models/MetadataPatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { EventListenerIn } from './EventListenerIn';
66
import type { LegacyEventListenerIn } from './LegacyEventListenerIn';
77

88
export type MetadataPatch = {
9-
context?: Array<string>;
9+
'@context'?: Array<string>;
1010
context_url?: string;
1111
definition?: string;
1212
content: any;

0 commit comments

Comments
 (0)