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,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)))
});