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: Define schema inside FmuResults model #970

Merged
merged 1 commit into from
Jan 14, 2025
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
24 changes: 24 additions & 0 deletions schemas/0.8.0/fmu_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@
"CaseMetadata": {
"description": "The FMU metadata model for an FMU case.\n\nA case represent a set of iterations that belong together, either by being part of\nthe same run (i.e. history matching) or by being placed together by the user,\ncorresponding to /scratch/<asset>/<user>/<my case name>/.",
"properties": {
"$schema": {
"format": "uri",
"minLength": 1,
"title": "$Schema",
"type": "string"
},
"access": {
"$ref": "#/$defs/Access"
},
Expand Down Expand Up @@ -3529,6 +3535,12 @@
"IterationMetadata": {
"description": "The FMU metadata model for an FMU Iteration.\n\nAn object representing a single Iteration of a specific case.",
"properties": {
"$schema": {
"format": "uri",
"minLength": 1,
"title": "$Schema",
"type": "string"
},
"access": {
"$ref": "#/$defs/Access"
},
Expand Down Expand Up @@ -4614,6 +4626,12 @@
"ObjectMetadata": {
"description": "The FMU metadata model for a given data object.",
"properties": {
"$schema": {
"format": "uri",
"minLength": 1,
"title": "$Schema",
"type": "string"
},
"access": {
"$ref": "#/$defs/SsdlAccess"
},
Expand Down Expand Up @@ -6454,6 +6472,12 @@
"RealizationMetadata": {
"description": "The FMU metadata model for an FMU Realization.\n\nAn object representing a single Realization of a specific Iteration.",
"properties": {
"$schema": {
"format": "uri",
"minLength": 1,
"title": "$Schema",
"type": "string"
},
"access": {
"$ref": "#/$defs/Access"
},
Expand Down
21 changes: 3 additions & 18 deletions src/fmu/dataio/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@

from typing import TYPE_CHECKING, Final, List, Literal, Optional, Union

from pydantic import (
AnyHttpUrl,
BaseModel,
Field,
)
from pydantic import Field

from ._logging import null_logger
from ._models.fmu_results import data, fields
from ._models.fmu_results.enums import FMUClass
from ._models.fmu_results.fmu_results import (
CaseMetadata,
FmuResultsSchema,
ObjectMetadata,
)
from ._models.fmu_results.global_configuration import GlobalConfiguration
Expand All @@ -42,17 +37,7 @@
logger: Final = null_logger(__name__)


class JsonSchemaMetadata(BaseModel):
"""Mixin to inject the $schema field into exported metadata."""

schema_: AnyHttpUrl = Field(
default_factory=lambda: AnyHttpUrl(FmuResultsSchema.url()),
alias="$schema",
frozen=True,
)


class ObjectMetadataExport(JsonSchemaMetadata, ObjectMetadata, populate_by_name=True):
class ObjectMetadataExport(ObjectMetadata, populate_by_name=True):
"""Wraps the schema ObjectMetadata, adjusting some values to optional for pragmatic
purposes when exporting metadata."""

Expand All @@ -65,7 +50,7 @@ class ObjectMetadataExport(JsonSchemaMetadata, ObjectMetadata, populate_by_name=
preprocessed: Optional[bool] = Field(alias="_preprocessed", default=None)


class CaseMetadataExport(JsonSchemaMetadata, CaseMetadata, populate_by_name=True):
class CaseMetadataExport(CaseMetadata, populate_by_name=True):
"""Adds the optional description field for backward compatibility."""

class_: Literal[FMUClass.case] = Field(
Expand Down
7 changes: 7 additions & 0 deletions src/fmu/dataio/_models/fmu_results/fmu_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING, Literal, Union

from pydantic import (
AnyHttpUrl,
BaseModel,
Field,
GetJsonSchemaHandler,
Expand Down Expand Up @@ -131,6 +132,12 @@ class MetadataBase(BaseModel):
version: str = Field(default=FmuResultsSchema.VERSION)
"""The version of the schema that generated this data."""

schema_: AnyHttpUrl = Field(
default_factory=lambda: AnyHttpUrl(FmuResultsSchema.url()),
alias="$schema",
)
"""The url of the schema that generated this data."""


class CaseMetadata(MetadataBase):
"""The FMU metadata model for an FMU case.
Expand Down
10 changes: 9 additions & 1 deletion tests/test_units/test_metadata_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
OperatingSystem,
TracklogEvent,
)
from fmu.dataio._utils import prettyprint_dict
from fmu.dataio._utils import prettyprint_dict, read_metadata_from_file
from fmu.dataio.providers.objectdata._provider import objectdata_provider_factory

# pylint: disable=no-member
Expand All @@ -33,6 +33,14 @@ def test_metadata_dollars(edataobj1, regsurf):
assert mymeta["$schema"] == FmuResultsSchema.url()
assert mymeta["source"] == FmuResultsSchema.SOURCE

# also check that it is preserved in the exported metadata
exportpath = edataobj1.export(regsurf)
exportmeta = read_metadata_from_file(exportpath)

assert exportmeta["version"] == FmuResultsSchema.VERSION
assert exportmeta["$schema"] == FmuResultsSchema.url()
assert exportmeta["source"] == FmuResultsSchema.SOURCE


# --------------------------------------------------------------------------------------
# Tracklog
Expand Down
Loading