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

H-345: Fix hash-graph-client class names #2872

Merged
merged 18 commits into from
Aug 8, 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
9 changes: 9 additions & 0 deletions apps/hash-graph/lib/graph/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ fn main() {
"--rust-out-dir",
&out_dir,
// Uncomment this to generate JSON Schema definitions in the `OUT_DIR` as well
// Be aware that when re-generating the JSON Schema definitions, you will need to do
// some manual work, to make sure it's compliant with OpenAPI 3.0
// This includes:
// * remove `$schema` and `$comment`,
// * rename `Record<string, any>` to `Object`,
// * move `definitions` to `status_definitions.json` (under the `definitions` key),
// * make sure all refs in `status.json` point to it
// This is a chore (I know), but otherwise openapi-generator-cli will fail to generate
// the client :/
// "--json-schema-out-dir",
// &out_dir,
])
Expand Down
35 changes: 17 additions & 18 deletions apps/hash-graph/lib/graph/src/api/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ async fn serve_static_schema(Path(path): Path<String>) -> Result<Response, Statu
&OperationGraphTagAddon,
&FilterSchemaAddon,
&TimeSchemaAddon,
&OntologyTypeSchemaAddon,
),
components(
schemas(
Expand Down Expand Up @@ -382,6 +381,9 @@ impl Modify for MergeAddon {
/// Any component that starts with `VAR_` will transform into a relative URL in the schema and
/// receive a `.json` ending.
///
/// Any component that starts with `SHARED_` will transform into a relative URL into the
/// `./models/shared.json` file.
///
/// For example the `VAR_Entity` component will be transformed into `./models/Entity.json`
struct ExternalRefAddon;

Expand Down Expand Up @@ -428,21 +430,32 @@ fn modify_schema_references(schema_component: &mut RefOr<openapi::Schema>) {
openapi::Schema::OneOf(one_of) => {
one_of.items.iter_mut().for_each(modify_schema_references);
}
openapi::Schema::AllOf(all_of) => {
all_of.items.iter_mut().for_each(modify_schema_references);
}
_ => (),
},
}
}

fn modify_reference(reference: &mut openapi::Ref) {
static REF_PREFIX: &str = "#/components/schemas/VAR_";
static REF_PREFIX_MODELS: &str = "#/components/schemas/VAR_";
static REF_PREFIX_SHARED: &str = "#/components/schemas/SHARED_";

if reference.ref_location.starts_with(REF_PREFIX) {
if reference.ref_location.starts_with(REF_PREFIX_MODELS) {
reference
.ref_location
.replace_range(0..REF_PREFIX.len(), "./models/");
.replace_range(0..REF_PREFIX_MODELS.len(), "./models/");
reference.ref_location.make_ascii_lowercase();
reference.ref_location.push_str(".json");
};

if reference.ref_location.starts_with(REF_PREFIX_SHARED) {
reference.ref_location.replace_range(
0..REF_PREFIX_SHARED.len(),
"./models/shared.json#/definitions/",
);
};
}

/// Append a `Graph` tag wherever a tag appears in individual routes.
Expand Down Expand Up @@ -673,17 +686,3 @@ impl Modify for TimeSchemaAddon {
}
}
}

/// Adds time-related structs to the `OpenAPI` schema.
struct OntologyTypeSchemaAddon;

impl Modify for OntologyTypeSchemaAddon {
fn modify(&self, openapi: &mut openapi::OpenApi) {
if let Some(ref mut components) = openapi.components {
components.schemas.insert(
"BaseUrl".to_owned(),
ObjectBuilder::new().schema_type(SchemaType::String).into(),
);
}
}
}
6 changes: 3 additions & 3 deletions apps/hash-graph/lib/graph/src/api/rest/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ where
#[derive(Debug, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
struct LoadExternalDataTypeRequest {
#[schema(value_type = String)]
#[schema(value_type = SHARED_VersionedUrl)]
data_type_id: VersionedUrl,
actor_id: RecordCreatedById,
}
Expand Down Expand Up @@ -343,7 +343,7 @@ async fn update_data_type<P: StorePool + Send>(
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
struct ArchiveDataTypeRequest {
#[schema(value_type = String)]
#[schema(value_type = SHARED_VersionedUrl)]
type_to_archive: VersionedUrl,
actor_id: RecordArchivedById,
}
Expand Down Expand Up @@ -399,7 +399,7 @@ async fn archive_data_type<P: StorePool + Send>(
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
struct UnarchiveDataTypeRequest {
#[schema(value_type = String)]
#[schema(value_type = SHARED_VersionedUrl)]
type_to_unarchive: VersionedUrl,
actor_id: RecordCreatedById,
}
Expand Down
6 changes: 3 additions & 3 deletions apps/hash-graph/lib/graph/src/api/rest/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl RoutedResource for EntityResource {
#[serde(rename_all = "camelCase")]
struct CreateEntityRequest {
properties: EntityProperties,
#[schema(value_type = String)]
#[schema(value_type = SHARED_VersionedUrl)]
entity_type_id: VersionedUrl,
owned_by_id: OwnedById,
#[schema(nullable = false)]
Expand Down Expand Up @@ -182,12 +182,12 @@ async fn get_entities_by_query<P: StorePool + Send>(
.map(|subgraph| Json(subgraph.into()))
}

#[derive(Debug, ToSchema, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
struct UpdateEntityRequest {
properties: EntityProperties,
entity_id: EntityId,
#[schema(value_type = String)]
#[schema(value_type = SHARED_VersionedUrl)]
entity_type_id: VersionedUrl,
actor_id: RecordCreatedById,
#[serde(flatten)]
Expand Down
10 changes: 6 additions & 4 deletions apps/hash-graph/lib/graph/src/api/rest/entity_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ struct CreateEntityTypeRequest {
owned_by_id: OwnedById,
actor_id: RecordCreatedById,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schema(value_type = SHARED_BaseUrl)]
label_property: Option<BaseUrl>,
}

Expand Down Expand Up @@ -292,7 +293,7 @@ where
#[derive(Debug, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
struct LoadExternalEntityTypeRequest {
#[schema(value_type = String)]
#[schema(value_type = SHARED_VersionedUrl)]
entity_type_id: VersionedUrl,
actor_id: RecordCreatedById,
}
Expand Down Expand Up @@ -423,10 +424,11 @@ async fn get_entity_types_by_query<P: StorePool + Send>(
struct UpdateEntityTypeRequest {
#[schema(value_type = VAR_UPDATE_ENTITY_TYPE)]
schema: serde_json::Value,
#[schema(value_type = String)]
#[schema(value_type = SHARED_VersionedUrl)]
type_to_update: VersionedUrl,
actor_id: RecordCreatedById,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schema(value_type = SHARED_BaseUrl)]
label_property: Option<BaseUrl>,
}

Expand Down Expand Up @@ -489,7 +491,7 @@ async fn update_entity_type<P: StorePool + Send>(
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
struct ArchiveEntityTypeRequest {
#[schema(value_type = String)]
#[schema(value_type = SHARED_VersionedUrl)]
type_to_archive: VersionedUrl,
actor_id: RecordArchivedById,
}
Expand Down Expand Up @@ -545,7 +547,7 @@ async fn archive_entity_type<P: StorePool + Send>(
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
struct UnarchiveEntityTypeRequest {
#[schema(value_type = String)]
#[schema(value_type = SHARED_VersionedUrl)]
type_to_unarchive: VersionedUrl,
actor_id: RecordCreatedById,
}
Expand Down
13 changes: 2 additions & 11 deletions apps/hash-graph/lib/graph/src/api/rest/json_schemas/data_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"enum": ["dataType"]
},
"$id": {
"$ref": "#/definitions/VersionedUrl"
"$ref": "./shared.json#/definitions/VersionedUrl"
},
"title": {
"type": "string"
Expand All @@ -27,14 +27,5 @@
}
},
"required": ["$schema", "kind", "$id", "title", "type"],
"additionalProperties": true,
"definitions": {
"VersionedUrl": {
"title": "Versioned URL",
"description": "The versioned URL of a Block Protocol ontology type (the $id of the schema). It should be of the form `${baseUrl}v/${versionNumber}`",
"type": "string",
"format": "uri",
"maxLength": 2048
}
}
"additionalProperties": true
}
131 changes: 6 additions & 125 deletions apps/hash-graph/lib/graph/src/api/rest/json_schemas/entity_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"enum": ["entityType"]
},
"$id": {
"$ref": "#/definitions/VersionedUrl"
"$ref": "./shared.json#/definitions/VersionedUrl"
},
"type": {
"type": "string",
Expand All @@ -29,7 +29,7 @@
"allOf": {
"type": "array",
"items": {
"$ref": "#/definitions/EntityTypeReference"
"$ref": "./shared.json#/definitions/EntityTypeReference"
}
},
"examples": {
Expand All @@ -39,137 +39,18 @@
}
},
"properties": {
"$ref": "#/definitions/PropertyTypeObject"
"$ref": "./shared.json#/definitions/PropertyTypeObject"
},
"required": {
"type": "array",
"items": {
"$ref": "#/definitions/BaseUrl"
"$ref": "./shared.json#/definitions/BaseUrl"
}
},
"links": {
"$ref": "#/definitions/LinkTypeObject"
"$ref": "./shared.json#/definitions/LinkTypeObject"
}
},
"additionalProperties": false,
"required": ["$schema", "kind", "type", "$id", "title", "properties"],
"definitions": {
"VersionedUrl": {
"title": "Versioned URL",
"description": "The versioned URL of a Block Protocol ontology type (the $id of the schema). It should be of the form `${baseUrl}v/${versionNumber}`",
"type": "string",
"format": "uri",
"maxLength": 2048
},
"BaseUrl": {
"title": "Base URL",
"description": "The base URL of a Block Protocol ontology type (the $id of the schema, without the versioned suffix). It should a valid URL, with a trailing slash.",
"type": "string",
"format": "uri",
"maxLength": 2048
},
"EntityTypeReference": {
"type": "object",
"properties": {
"$ref": {
"$ref": "#/definitions/VersionedUrl"
}
},
"required": ["$ref"],
"additionalProperties": false
},
"PropertyObjectReference": {
"type": "object",
"properties": {
"$ref": {
"$ref": "#/definitions/BaseUrl"
}
},
"required": ["$ref"],
"additionalProperties": false
},
"PropertyTypeObject": {
"title": "Property Type Object",
"description": "A JSON object where each entry is constrained by a property type.",
"type": "object",
"propertyNames": {
"$ref": "https://blockprotocol.org/types/modules/graph/0.3/schema/base-url"
},
"x-patternProperties": {
".*": {
"oneOf": [
{
"$ref": "#/definitions/PropertyObjectReference"
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["array"]
},
"items": {
"$ref": "#/definitions/PropertyObjectReference"
},
"minItems": {
"type": "integer",
"minimum": 0
},
"maxItems": {
"type": "integer",
"minimum": 0
}
},
"required": ["type", "items"],
"additionalProperties": false
}
]
}
}
},
"LinkTypeObject": {
"type": "object",
"x-propertyNames": {
"$ref": "#/definitions/VersionedUrl"
},
"x-patternProperties": {
".*": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["array"]
},
"ordered": {
"type": "boolean",
"default": false
},
"items": {
"description": "Specifies a set of entity types inside a oneOf",
"type": "object",
"properties": {
"oneOf": {
"type": "array",
"items": {
"$ref": "#/definitions/EntityTypeReference"
}
}
},
"additionalProperties": false
},
"minItems": {
"type": "integer",
"minimum": 0
},
"maxItems": {
"type": "integer",
"minimum": 0
}
},
"required": ["type", "ordered", "items"],
"additionalProperties": false
}
}
}
}
"required": ["$schema", "kind", "type", "$id", "title", "properties"]
}
Loading