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

feat: assert whether generated and versioned schema are identical #2

Closed
wants to merge 3 commits into from
Closed
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: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ jobs:
cargo test
cargo clippy -- -Dwarnings

generate_schema:
validate_schema:
runs-on: ubuntu-latest
needs: build_and_test
if: github.ref == 'refs/heads/main'
# if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- name: Generate schema
- name: Test schema
run: |
cp schema/data-model-schema.json schema/data-model-schema.json.orig
cargo run
# assert the generated and versioned file equal
diff -u schema/data-model-schema.json schema/data-model-schema.json.orig
95 changes: 61 additions & 34 deletions schema/data-model-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
],
"items": {
"$ref": "#/definitions/DataModelExtension"
}
},
"nullable": true
},
"id": {
"$ref": "#/definitions/PfId"
Expand All @@ -55,7 +56,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"productCategoryCpc": {
"$ref": "#/definitions/NonEmptyString"
Expand All @@ -79,28 +81,32 @@
"type": [
"string",
"null"
]
],
"nullable": true
},
"updated": {
"type": [
"string",
"null"
],
"format": "date-time"
"format": "date-time",
"nullable": true
},
"validityPeriodEnd": {
"type": [
"string",
"null"
],
"format": "date-time"
"format": "date-time",
"nullable": true
},
"validityPeriodStart": {
"type": [
"string",
"null"
],
"format": "date-time"
"format": "date-time",
"nullable": true
},
"version": {
"$ref": "#/definitions/VersionInteger"
Expand All @@ -126,20 +132,23 @@
{
"type": "null"
}
]
],
"nullable": true
},
"comments": {
"type": [
"string",
"null"
]
],
"nullable": true
},
"completedAt": {
"type": [
"string",
"null"
],
"format": "date-time"
"format": "date-time",
"nullable": true
},
"coverage": {
"anyOf": [
Expand All @@ -149,7 +158,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"level": {
"anyOf": [
Expand All @@ -159,7 +169,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"providerName": {
"type": "string"
Expand All @@ -168,7 +179,8 @@
"type": [
"string",
"null"
]
],
"nullable": true
}
}
},
Expand Down Expand Up @@ -208,9 +220,6 @@
"description": "Data Type \"CarbonFootprint\" of Spec Version 2",
"type": "object",
"anyOf": [
{
"type": "null"
},
{
"type": "object",
"required": [
Expand Down Expand Up @@ -272,13 +281,15 @@
{
"type": "null"
}
]
],
"nullable": true
},
"allocationRulesDescription": {
"type": [
"string",
"null"
]
],
"nullable": true
},
"assurance": {
"anyOf": [
Expand All @@ -288,7 +299,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"biogenicAccountingMethodology": {
"anyOf": [
Expand All @@ -298,7 +310,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"biogenicCarbonContent": {
"$ref": "#/definitions/PositiveDecimal"
Expand All @@ -311,7 +324,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"boundaryProcessesDescription": {
"type": "string"
Expand All @@ -330,7 +344,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"declaredUnit": {
"$ref": "#/definitions/DeclaredUnit"
Expand All @@ -343,7 +358,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"exemptedEmissionsDescription": {
"type": "string"
Expand All @@ -365,7 +381,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"ipccCharacterizationFactorsSources": {
"$ref": "#/definitions/IpccCharacterizationFactorsSources"
Expand All @@ -378,7 +395,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"otherBiogenicGhgEmissions": {
"anyOf": [
Expand All @@ -388,7 +406,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"pCfExcludingBiogenic": {
"$ref": "#/definitions/PositiveDecimal"
Expand All @@ -401,7 +420,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"packagingEmissionsIncluded": {
"type": "boolean"
Expand All @@ -414,7 +434,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"primaryDataShare": {
"anyOf": [
Expand All @@ -424,7 +445,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"productOrSectorSpecificRules": {
"$ref": "#/definitions/ProductOrSectorSpecificRuleSet"
Expand All @@ -445,13 +467,15 @@
{
"type": "null"
}
]
],
"nullable": true
},
"uncertaintyAssessmentDescription": {
"type": [
"string",
"null"
]
],
"nullable": true
},
"unitaryProductAmount": {
"$ref": "#/definitions/StrictlyPositiveDecimal"
Expand Down Expand Up @@ -516,7 +540,8 @@
"type": [
"string",
"null"
]
],
"nullable": true
},
"specVersion": {
"$ref": "#/definitions/VersionString"
Expand Down Expand Up @@ -713,7 +738,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"ruleNames": {
"$ref": "#/definitions/NonEmptyStringVec"
Expand Down Expand Up @@ -748,7 +774,8 @@
{
"type": "null"
}
]
],
"nullable": true
},
"ruleNames": {
"$ref": "#/definitions/NonEmptyStringVec"
Expand Down Expand Up @@ -801,4 +828,4 @@
"pattern": "^\\d+\\.\\d+\\.\\d+(-\\d{8})?$"
}
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ pub struct VersionInteger(pub i32);
#[serde(untagged)]
/// Encoded geographic scope rules of a Spec Version 2 `CarbonFootprint`
pub enum GeographicScope {
#[serde(skip_serializing)]
#[schemars(skip)]
Global,
#[serde(rename_all = "camelCase")]
Regional {
Expand Down
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
use pact_data_model::*;
use schemars::schema_for;
use schemars::gen::{SchemaGenerator, SchemaSettings};
use serde_json::to_string_pretty;
use std::fs::File;
use std::io::{Error, Write};

fn main() -> Result<(), Error> {
let schema = schema_for!(ProductFootprint);
let mut openapi_settings = SchemaSettings::draft07();

let schema_json = to_string_pretty(&schema).expect("Failed to serialize schema");
openapi_settings.option_nullable = true;

let schema_generator = SchemaGenerator::from(openapi_settings);

let schema = schema_generator.into_root_schema_for::<ProductFootprint>();

let mut schema_json = to_string_pretty(&schema).expect("Failed to serialize schema");

if !schema_json.ends_with('\n') {
schema_json.push('\n');
}

let mut file = File::create("./schema/data-model-schema.json")?;

Expand Down
Loading