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

MAINT: Template schema version and add dev schema url #942

Merged
merged 1 commit into from
Dec 31, 2024
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
2 changes: 1 addition & 1 deletion schema/definitions/0.8.0/schema/fmu_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -10879,7 +10879,7 @@
"type": "object"
}
},
"$id": "fmu_results.json",
"$id": "https://main-fmu-schemas-prod.radix.equinor.com/schemas/0.8.0/fmu_results.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"discriminator": {
"propertyName": "class"
Expand Down
20 changes: 16 additions & 4 deletions src/fmu/dataio/_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@
from enum import Enum
from typing import Final

SCHEMA: Final = (
"https://main-fmu-schemas-prod.radix.equinor.com/schemas/0.8.0/fmu_results.json"
)
VERSION: Final = "0.8.0"

class FmuResultsSchema:
DEV_ROOT: Final[str] = "https://main-fmu-schemas-dev.radix.equinor.com/schemas"
PROD_ROOT: Final[str] = "https://main-fmu-schemas-prod.radix.equinor.com/schemas"
VERSION: Final[str] = "0.8.0"
FILENAME: Final[str] = "fmu_results.json"
DEV_URL: Final[str] = f"{DEV_ROOT}/{VERSION}/{FILENAME}"
PROD_URL: Final[str] = f"{PROD_ROOT}/{VERSION}/{FILENAME}"

@staticmethod
def url() -> str:
"""This method is meant to return the `PROD_URL` or `DEV_URL` under relevant
circumstances."""
return FmuResultsSchema.PROD_URL


SOURCE: Final = "fmu"


Expand Down
6 changes: 3 additions & 3 deletions src/fmu/dataio/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pydantic import AnyHttpUrl, TypeAdapter

from ._definitions import SCHEMA, SOURCE, VERSION
from ._definitions import SOURCE, FmuResultsSchema
from ._logging import null_logger
from ._model import fields, schema
from ._model.global_configuration import GlobalConfiguration
Expand Down Expand Up @@ -104,8 +104,8 @@ def generate_export_metadata(
objdata = objectdata_provider_factory(obj, dataio)

return schema.InternalObjectMetadata(
schema_=TypeAdapter(AnyHttpUrl).validate_strings(SCHEMA), # type: ignore[call-arg]
version=VERSION,
schema_=TypeAdapter(AnyHttpUrl).validate_strings(FmuResultsSchema.url()), # type: ignore[call-arg]
version=FmuResultsSchema.VERSION,
source=SOURCE,
class_=objdata.classname,
fmu=_get_meta_fmu(fmudata) if fmudata else None,
Expand Down
4 changes: 3 additions & 1 deletion src/fmu/dataio/_model/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from pydantic.json_schema import GenerateJsonSchema
from typing_extensions import Annotated

from fmu.dataio._definitions import FmuResultsSchema

from .data import AnyData
from .enums import FMUClass
from .fields import (
Expand Down Expand Up @@ -268,7 +270,7 @@ def generate(
) -> dict[str, Any]:
json_schema = super().generate(schema, mode=mode)
json_schema["$schema"] = self.schema_dialect
json_schema["$id"] = "fmu_results.json"
json_schema["$id"] = FmuResultsSchema.url()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it correct that the id is set to a url?

Copy link
Collaborator Author

@mferrera mferrera Dec 31, 2024

Choose a reason for hiding this comment

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

For a top-level schema, yep: ref.. Not the best naming...

Incidentally, this value is overwritten here anyway:

cat "$file" | jq '.["$id"]='\""$id"\" > "$sdir"/"$ffile"

But I'm hoping to make this a little more explicit in the code and eventually remove this shell script patch

json_schema["$contractual"] = self.contractual

# sumo-core's validator does not recognize these.
Expand Down
6 changes: 3 additions & 3 deletions src/fmu/dataio/_model/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
model_validator,
)

from fmu.dataio._definitions import SCHEMA, SOURCE, VERSION
from fmu.dataio._definitions import SOURCE, FmuResultsSchema

from . import data, enums, fields

Expand Down Expand Up @@ -102,9 +102,9 @@ def _validate_input(cls, values: dict) -> dict:
class JsonSchemaMetadata(BaseModel, populate_by_name=True):
schema_: AnyHttpUrl = Field(
alias="$schema",
default=TypeAdapter(AnyHttpUrl).validate_python(SCHEMA),
default=TypeAdapter(AnyHttpUrl).validate_python(FmuResultsSchema.PROD_URL),
)
version: str = Field(default=VERSION)
version: str = Field(default=FmuResultsSchema.VERSION)
source: str = Field(default=SOURCE)


Expand Down
12 changes: 4 additions & 8 deletions tests/test_units/test_metadata_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
import pytest

import fmu.dataio as dio
from fmu.dataio._metadata import (
SCHEMA,
SOURCE,
VERSION,
generate_export_metadata,
)
from fmu.dataio._definitions import SOURCE, FmuResultsSchema
from fmu.dataio._metadata import generate_export_metadata
from fmu.dataio._model import enums
from fmu.dataio._model.fields import (
OperatingSystem,
Expand All @@ -34,8 +30,8 @@ def test_metadata_dollars(edataobj1, regsurf):

mymeta = edataobj1.generate_metadata(obj=regsurf)

assert mymeta["version"] == VERSION
assert mymeta["$schema"] == SCHEMA
assert mymeta["version"] == FmuResultsSchema.VERSION
assert mymeta["$schema"] == FmuResultsSchema.PROD_URL
assert mymeta["source"] == SOURCE


Expand Down
9 changes: 5 additions & 4 deletions tools/update_schema
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import sys
from pathlib import Path
from typing import Any, Final

from fmu.dataio._definitions import FmuResultsSchema
from fmu.dataio._model import dump

GREEN = "\033[32m"
Expand All @@ -34,14 +35,14 @@ def _get_parser() -> argparse.ArgumentParser:
"-v",
type=str,
help=f"The version of the schema being output. Default is {SCHEMA_VERSION}",
default=SCHEMA_VERSION,
default=FmuResultsSchema.VERSION,
)
parser.add_argument(
"--filename",
"-f",
type=str,
help=f"The filename of the schema being output. Default is {SCHEMA_FILENAME}.",
default=SCHEMA_FILENAME,
default=FmuResultsSchema.FILENAME,
)
parser.add_argument(
"--diff",
Expand Down Expand Up @@ -75,8 +76,8 @@ def _load_json(filepath: Path) -> dict[str, Any]:
return json.load(f)
except json.JSONDecodeError as json_decode_error:
print(
f"{FAILURE} Parsing existing json schema failed: The json is malformed. "
"If you know why, re-run the command with argument '--force' "
f"{FAILURE} Parsing existing json schema failed: The json is malformed."
" If you know why, re-run the command with argument '--force' "
"to overwrite with the new schema.\n"
f"{FAILURE} Json parsing error: '{json_decode_error.msg}.'"
)
Expand Down
Loading