ci(validate-openapi-spec): fail check if OpenAPI spec is not up-to-date (#1582)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Sanchith Hegde
2023-07-04 11:29:59 +05:30
committed by GitHub
parent eabe16cc85
commit 7b21777bcf
6 changed files with 294 additions and 251 deletions

View File

@ -1,37 +0,0 @@
name: Validate Generated OpenAPI Spec File
on:
pull_request:
merge_group:
types:
- checks_requested
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
validate_json:
name: Validate generated openapi spec file
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
- name: Generate the spec file
shell: bash
run: cargo run --features openapi -- generate-openapi-spec
- name: Install swagger-cli
shell: bash
run: npm install -g @apidevtools/swagger-cli
- name: Validate the json file
shell: bash
run: swagger-cli validate ./openapi/openapi_spec.json

View File

@ -0,0 +1,55 @@
name: Validate Generated OpenAPI Spec File
on:
pull_request:
merge_group:
types:
- checks_requested
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
validate_json:
name: Validate generated OpenAPI spec file
runs-on: ubuntu-latest
steps:
- name: Checkout PR
if: ${{ github.event_name == 'pull_request' }}
uses: actions/checkout@v3
with:
# Checkout pull request branch instead of merge commit
ref: ${{ github.event.pull_request.head.ref }}
- name: Checkout merge group HEAD commit
if: ${{ github.event_name == 'merge_group' }}
uses: actions/checkout@v3
with:
ref: ${{ github.event.merge_group.head_sha }}
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
- name: Generate the OpenAPI spec file
shell: bash
run: cargo run --features openapi -- generate-openapi-spec
- name: Install `swagger-cli`
shell: bash
run: npm install -g @apidevtools/swagger-cli
- name: Validate the JSON file
shell: bash
run: swagger-cli validate ./openapi/openapi_spec.json
- name: Fail check if the JSON file is not up-to-date
shell: bash
run: |
if ! git diff --quiet --exit-code -- openapi/openapi_spec.json ; then
echo '::error::The OpenAPI spec file is not up-to-date. Please re-generate the OpenAPI spec file using `cargo run --features openapi -- generate-openapi-spec` and commit it.'
exit 1
fi

43
Cargo.lock generated
View File

@ -1602,7 +1602,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "907076dfda823b0b36d2a1bb5f90c96660a5bbcd7729e10727f07858f22c4edc"
dependencies = [
"cfg-if",
"hashbrown",
"hashbrown 0.12.3",
"lock_api",
"once_cell",
"parking_lot_core",
@ -1788,6 +1788,12 @@ dependencies = [
"termcolor",
]
[[package]]
name = "equivalent"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88bffebc5d80432c9b140ee17875ff173a8ab62faad5b257da912bd2f6c1c0a1"
[[package]]
name = "errno"
version = "0.3.1"
@ -2223,7 +2229,7 @@ dependencies = [
"futures-sink",
"futures-util",
"http",
"indexmap",
"indexmap 1.9.3",
"slab",
"tokio",
"tokio-util",
@ -2239,6 +2245,12 @@ dependencies = [
"ahash 0.7.6",
]
[[package]]
name = "hashbrown"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
[[package]]
name = "heck"
version = "0.4.1"
@ -2458,10 +2470,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown",
"hashbrown 0.12.3",
"serde",
]
[[package]]
name = "indexmap"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d"
dependencies = [
"equivalent",
"hashbrown 0.14.0",
]
[[package]]
name = "infer"
version = "0.2.3"
@ -3068,7 +3090,7 @@ dependencies = [
"fnv",
"futures-channel",
"futures-util",
"indexmap",
"indexmap 1.9.3",
"once_cell",
"pin-project-lite",
"thiserror",
@ -3104,7 +3126,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a"
dependencies = [
"dlv-list",
"hashbrown",
"hashbrown 0.12.3",
]
[[package]]
@ -3815,6 +3837,7 @@ version = "0.1.0"
dependencies = [
"darling 0.14.4",
"diesel",
"indexmap 2.0.0",
"proc-macro2",
"quote",
"serde",
@ -4081,7 +4104,7 @@ version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1"
dependencies = [
"indexmap",
"indexmap 1.9.3",
"itoa",
"ryu",
"serde",
@ -4168,7 +4191,7 @@ dependencies = [
"base64 0.21.2",
"chrono",
"hex",
"indexmap",
"indexmap 1.9.3",
"serde",
"serde_json",
"serde_with_macros",
@ -4751,7 +4774,7 @@ version = "0.19.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2380d56e8670370eee6566b0bfd4265f65b3f432e8c6d85623f728d4fa31f739"
dependencies = [
"indexmap",
"indexmap 1.9.3",
"serde",
"serde_spanned",
"toml_datetime",
@ -4798,7 +4821,7 @@ checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c"
dependencies = [
"futures-core",
"futures-util",
"indexmap",
"indexmap 1.9.3",
"pin-project",
"pin-project-lite",
"rand 0.8.5",
@ -5055,7 +5078,7 @@ version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68ae74ef183fae36d650f063ae7bde1cacbe1cd7e72b617cbe1e985551878b98"
dependencies = [
"indexmap",
"indexmap 1.9.3",
"serde",
"serde_json",
"utoipa-gen",

View File

@ -13,6 +13,7 @@ doctest = false
[dependencies]
darling = "0.14.4"
indexmap = "2.0.0"
proc-macro2 = "1.0.56"
quote = "1.0.26"
syn = { version = "1.0.109", features = ["full", "extra-traits"] } # the full feature does not seem to encompass all the features

View File

@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use indexmap::IndexMap;
use syn::{self, parse_quote, punctuated::Punctuated, Token};
use crate::macros::helpers;
@ -45,9 +46,9 @@ pub fn polymorphic_macro_derive_inner(
// Go through all the fields and create a mapping of required fields for a schema
// PaymentsCreate -> ["amount","currency"]
// This will be stored in a hashset
// mandatory_hashset -> ((PaymentsCreate, amount), (PaymentsCreate,currency))
let mut mandatory_hashset = HashSet::<(syn::Ident, syn::Ident)>::new();
let mut other_fields_hm = HashMap::<syn::Field, Vec<syn::Attribute>>::new();
// required_fields -> ((amount, PaymentsCreate), (currency, PaymentsCreate))
let mut required_fields = HashSet::<(syn::Ident, syn::Ident)>::new();
let mut all_fields = IndexMap::<syn::Field, Vec<syn::Attribute>>::new();
fields.iter().for_each(|field| {
// Partition the attributes of a field into two vectors
@ -68,7 +69,7 @@ pub fn polymorphic_macro_derive_inner(
let mut field_without_attributes = field.clone();
field_without_attributes.attrs.clear();
other_fields_hm
all_fields
.entry(field_without_attributes.to_owned())
.or_insert(vec![])
.push(attribute.to_owned().to_owned());
@ -76,8 +77,8 @@ pub fn polymorphic_macro_derive_inner(
// Mandatory attributes are to be inserted into hashset
// The hashset will store it in this format
// (PaymentsCreateRequest, "amount")
// (PaymentsConfirmRequest, "currency")
// ("amount", PaymentsCreateRequest)
// ("currency", PaymentsConfirmRequest)
//
// For these attributes, we need to later add #[schema(required = true)] attribute
_ = mandatory_attribute
@ -91,7 +92,7 @@ pub fn polymorphic_macro_derive_inner(
.filter_map(|schema| field.ident.to_owned().zip(Some(schema.to_owned())))
.collect::<HashSet<_>>();
mandatory_hashset.extend(res);
required_fields.extend(res);
Ok::<_, syn::Error>(())
});
});
@ -99,7 +100,7 @@ pub fn polymorphic_macro_derive_inner(
let schemas = schemas_to_create
.iter()
.map(|schema| {
let fields = other_fields_hm
let fields = all_fields
.iter()
.flat_map(|(field, value)| {
let mut attributes = value
@ -114,7 +115,7 @@ pub fn polymorphic_macro_derive_inner(
// Can be none, because tuple fields have no ident
field.ident.to_owned().and_then(|field_ident| {
mandatory_hashset
required_fields
.contains(&(field_ident, schema.to_owned()))
.then(|| attributes.push(quote::quote!(#required_attribute)))
});

View File

@ -6344,21 +6344,20 @@
"manual_retry"
],
"properties": {
"capture_method": {
"allOf": [
{
"$ref": "#/components/schemas/CaptureMethod"
}
],
"nullable": true
"payment_id": {
"type": "string",
"description": "Unique identifier for the payment. This ensures idempotency for multiple payments\nthat have been done by a single merchant. This field is auto generated and is returned in the API response.",
"example": "pay_mbabizu24mvu3mela5njyhpit4",
"nullable": true,
"maxLength": 30,
"minLength": 30
},
"payment_experience": {
"allOf": [
{
"$ref": "#/components/schemas/PaymentExperience"
}
],
"nullable": true
"merchant_id": {
"type": "string",
"description": "This is an identifier for the merchant account. This is inferred from the API key\nprovided during the request",
"example": "merchant_1668273825",
"nullable": true,
"maxLength": 255
},
"amount": {
"type": "integer",
@ -6368,20 +6367,12 @@
"nullable": true,
"minimum": 0.0
},
"statement_descriptor_name": {
"type": "string",
"description": "For non-card charges, you can use this value as the complete description that appears on your customers statements. Must contain at least one letter, maximum 22 characters.",
"example": "Hyperswitch Router",
"nullable": true,
"maxLength": 255
},
"order_details": {
"type": "array",
"items": {
"$ref": "#/components/schemas/OrderDetailsWithAmount"
},
"description": "Information about the product , quantity and amount for connectors. (e.g. Klarna)",
"example": "[{\n \"product_name\": \"gillete creme\",\n \"quantity\": 15,\n \"amount\" : 900\n }]",
"routing": {
"allOf": [
{
"$ref": "#/components/schemas/RoutingAlgorithm"
}
],
"nullable": true
},
"connector": {
@ -6404,6 +6395,35 @@
],
"nullable": true
},
"capture_method": {
"allOf": [
{
"$ref": "#/components/schemas/CaptureMethod"
}
],
"nullable": true
},
"amount_to_capture": {
"type": "integer",
"format": "int64",
"description": "The Amount to be captured/ debited from the users payment method. It shall be in lowest denomination of the currency. (i.e) in cents for USD denomination, in paisa for INR denomination etc.,\nIf not provided, the default amount_to_capture will be the payment amount.",
"example": 6540,
"nullable": true
},
"capture_on": {
"type": "string",
"format": "date-time",
"description": "A timestamp (ISO 8601 code) that determines when the payment should be captured.\nProviding this field will automatically set `capture` to true",
"example": "2022-09-10T10:11:12Z",
"nullable": true
},
"confirm": {
"type": "boolean",
"description": "Whether to confirm the payment (if applicable)",
"default": false,
"example": true,
"nullable": true
},
"customer": {
"allOf": [
{
@ -6412,37 +6432,26 @@
],
"nullable": true
},
"mandate_data": {
"allOf": [
{
"$ref": "#/components/schemas/MandateData"
}
],
"nullable": true
},
"browser_info": {
"type": "object",
"description": "Additional details required by 3DS 2.0",
"nullable": true
},
"business_label": {
"customer_id": {
"type": "string",
"description": "Business label of the merchant for this payment",
"example": "food",
"nullable": true
"description": "The identifier for the customer object.\nThis field will be deprecated soon, use the customer object instead",
"example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
"nullable": true,
"maxLength": 255
},
"payment_method": {
"allOf": [
{
"$ref": "#/components/schemas/PaymentMethod"
}
],
"nullable": true
},
"card_cvc": {
"email": {
"type": "string",
"description": "This is used when payment is to be confirmed and the card is not saved",
"nullable": true
"description": "The customer's email address\nThis field will be deprecated soon, use the customer object instead",
"example": "johntest@test.com",
"nullable": true,
"maxLength": 255
},
"name": {
"type": "string",
"description": "description: The customer's name\nThis field will be deprecated soon, use the customer object instead",
"example": "John Test",
"nullable": true,
"maxLength": 255
},
"phone": {
"type": "string",
@ -6451,23 +6460,23 @@
"nullable": true,
"maxLength": 255
},
"authentication_type": {
"allOf": [
{
"$ref": "#/components/schemas/AuthenticationType"
}
],
"phone_country_code": {
"type": "string",
"description": "The country code for the customer phone number\nThis field will be deprecated soon, use the customer object instead",
"example": "+1",
"nullable": true,
"maxLength": 255
},
"off_session": {
"type": "boolean",
"description": "Set to true to indicate that the customer is not in your checkout flow during this payment, and therefore is unable to authenticate. This parameter is intended for scenarios where you collect card details and charge them later. This parameter can only be used with `confirm: true`.",
"example": true,
"nullable": true
},
"manual_retry": {
"type": "boolean",
"description": "If enabled payment can be retried from the client side until the payment is successful or payment expires or the attempts(configured by the merchant) for payment are exhausted."
},
"capture_on": {
"description": {
"type": "string",
"format": "date-time",
"description": "A timestamp (ISO 8601 code) that determines when the payment should be captured.\nProviding this field will automatically set `capture` to true",
"example": "2022-09-10T10:11:12Z",
"description": "A description of the payment",
"example": "It's my first payment request",
"nullable": true
},
"return_url": {
@ -6484,6 +6493,41 @@
],
"nullable": true
},
"authentication_type": {
"allOf": [
{
"$ref": "#/components/schemas/AuthenticationType"
}
],
"nullable": true
},
"payment_method_data": {
"allOf": [
{
"$ref": "#/components/schemas/PaymentMethodData"
}
],
"nullable": true
},
"payment_method": {
"allOf": [
{
"$ref": "#/components/schemas/PaymentMethod"
}
],
"nullable": true
},
"payment_token": {
"type": "string",
"description": "Provide a reference to a stored payment method",
"example": "187282ab-40ef-47a9-9206-5099ba31e432",
"nullable": true
},
"card_cvc": {
"type": "string",
"description": "This is used when payment is to be confirmed and the card is not saved",
"nullable": true
},
"shipping": {
"allOf": [
{
@ -6492,18 +6536,6 @@
],
"nullable": true
},
"business_sub_label": {
"type": "string",
"description": "Business sub label for the payment",
"nullable": true
},
"confirm": {
"type": "boolean",
"description": "Whether to confirm the payment (if applicable)",
"default": false,
"example": true,
"nullable": true
},
"billing": {
"allOf": [
{
@ -6512,97 +6544,17 @@
],
"nullable": true
},
"routing": {
"allOf": [
{
"$ref": "#/components/schemas/RoutingAlgorithm"
}
],
"nullable": true
},
"amount_to_capture": {
"type": "integer",
"format": "int64",
"description": "The Amount to be captured/ debited from the users payment method. It shall be in lowest denomination of the currency. (i.e) in cents for USD denomination, in paisa for INR denomination etc.,\nIf not provided, the default amount_to_capture will be the payment amount.",
"example": 6540,
"nullable": true
},
"off_session": {
"type": "boolean",
"description": "Set to true to indicate that the customer is not in your checkout flow during this payment, and therefore is unable to authenticate. This parameter is intended for scenarios where you collect card details and charge them later. This parameter can only be used with `confirm: true`.",
"example": true,
"nullable": true
},
"customer_id": {
"statement_descriptor_name": {
"type": "string",
"description": "The identifier for the customer object.\nThis field will be deprecated soon, use the customer object instead",
"example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
"description": "For non-card charges, you can use this value as the complete description that appears on your customers statements. Must contain at least one letter, maximum 22 characters.",
"example": "Hyperswitch Router",
"nullable": true,
"maxLength": 255
},
"payment_method_type": {
"allOf": [
{
"$ref": "#/components/schemas/PaymentMethodType"
}
],
"nullable": true
},
"name": {
"statement_descriptor_suffix": {
"type": "string",
"description": "description: The customer's name\nThis field will be deprecated soon, use the customer object instead",
"example": "John Test",
"nullable": true,
"maxLength": 255
},
"payment_id": {
"type": "string",
"description": "Unique identifier for the payment. This ensures idempotency for multiple payments\nthat have been done by a single merchant. This field is auto generated and is returned in the API response.",
"example": "pay_mbabizu24mvu3mela5njyhpit4",
"nullable": true,
"maxLength": 30,
"minLength": 30
},
"payment_method_data": {
"allOf": [
{
"$ref": "#/components/schemas/PaymentMethodData"
}
],
"nullable": true
},
"client_secret": {
"type": "string",
"description": "It's a token used for client side verification.",
"example": "pay_U42c409qyHwOkWo3vK60_secret_el9ksDkiB8hi6j9N78yo",
"nullable": true
},
"email": {
"type": "string",
"description": "The customer's email address\nThis field will be deprecated soon, use the customer object instead",
"example": "johntest@test.com",
"nullable": true,
"maxLength": 255
},
"mandate_id": {
"type": "string",
"description": "A unique identifier to link the payment to a mandate, can be use instead of payment_method_data",
"example": "mandate_iwer89rnjef349dni3",
"nullable": true,
"maxLength": 255
},
"business_country": {
"allOf": [
{
"$ref": "#/components/schemas/CountryAlpha2"
}
],
"nullable": true
},
"merchant_id": {
"type": "string",
"description": "This is an identifier for the merchant account. This is inferred from the API key\nprovided during the request",
"example": "merchant_1668273825",
"description": "Provides information about a card payment that customers see on their statements. Concatenated with the prefix (shortened descriptor) or statement descriptor thats set on the account to form the complete statement descriptor. Maximum 22 characters for the concatenated descriptor.",
"example": "Payment for shoes purchase",
"nullable": true,
"maxLength": 255
},
@ -6614,16 +6566,69 @@
],
"nullable": true
},
"description": {
"type": "string",
"description": "A description of the payment",
"example": "It's my first payment request",
"order_details": {
"type": "array",
"items": {
"$ref": "#/components/schemas/OrderDetailsWithAmount"
},
"description": "Information about the product , quantity and amount for connectors. (e.g. Klarna)",
"example": "[{\n \"product_name\": \"gillete creme\",\n \"quantity\": 15,\n \"amount\" : 900\n }]",
"nullable": true
},
"payment_token": {
"client_secret": {
"type": "string",
"description": "Provide a reference to a stored payment method",
"example": "187282ab-40ef-47a9-9206-5099ba31e432",
"description": "It's a token used for client side verification.",
"example": "pay_U42c409qyHwOkWo3vK60_secret_el9ksDkiB8hi6j9N78yo",
"nullable": true
},
"mandate_data": {
"allOf": [
{
"$ref": "#/components/schemas/MandateData"
}
],
"nullable": true
},
"mandate_id": {
"type": "string",
"description": "A unique identifier to link the payment to a mandate, can be use instead of payment_method_data",
"example": "mandate_iwer89rnjef349dni3",
"nullable": true,
"maxLength": 255
},
"browser_info": {
"type": "object",
"description": "Additional details required by 3DS 2.0",
"nullable": true
},
"payment_experience": {
"allOf": [
{
"$ref": "#/components/schemas/PaymentExperience"
}
],
"nullable": true
},
"payment_method_type": {
"allOf": [
{
"$ref": "#/components/schemas/PaymentMethodType"
}
],
"nullable": true
},
"business_country": {
"allOf": [
{
"$ref": "#/components/schemas/CountryAlpha2"
}
],
"nullable": true
},
"business_label": {
"type": "string",
"description": "Business label of the merchant for this payment",
"example": "food",
"nullable": true
},
"merchant_connector_details": {
@ -6634,25 +6639,6 @@
],
"nullable": true
},
"udf": {
"type": "object",
"description": "Any user defined fields can be passed here.",
"nullable": true
},
"phone_country_code": {
"type": "string",
"description": "The country code for the customer phone number\nThis field will be deprecated soon, use the customer object instead",
"example": "+1",
"nullable": true,
"maxLength": 255
},
"statement_descriptor_suffix": {
"type": "string",
"description": "Provides information about a card payment that customers see on their statements. Concatenated with the prefix (shortened descriptor) or statement descriptor thats set on the account to form the complete statement descriptor. Maximum 22 characters for the concatenated descriptor.",
"example": "Payment for shoes purchase",
"nullable": true,
"maxLength": 255
},
"allowed_payment_method_types": {
"type": "array",
"items": {
@ -6660,6 +6646,20 @@
},
"description": "Allowed Payment Method Types for a given PaymentIntent",
"nullable": true
},
"business_sub_label": {
"type": "string",
"description": "Business sub label for the payment",
"nullable": true
},
"manual_retry": {
"type": "boolean",
"description": "If enabled payment can be retried from the client side until the payment is successful or payment expires or the attempts(configured by the merchant) for payment are exhausted."
},
"udf": {
"type": "object",
"description": "Any user defined fields can be passed here.",
"nullable": true
}
}
},