Skip to content

Commit

Permalink
check has_fields before retain
Browse files Browse the repository at this point in the history
  • Loading branch information
cataggar committed Oct 5, 2023
1 parent 8f39a2d commit 060ad1a
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions services/autorust/codegen/src/codegen_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,30 +161,20 @@ impl SchemaGen {
self.all_of.iter().collect()
}

/// Get the number of fields in the struct.
fn len_fields(&self) -> usize {
self.all_of().len() + self.properties.len()
}

/// Get the number of fields in the struct, excluding the discriminator.
fn len_fields_without_descriminator(&self) -> usize {
let mut len = self.len_fields();
fn len_fields(&self) -> usize {
let mut len = self.all_of().len() + self.properties.len();
if self.discriminator().is_some() {
len = len.saturating_sub(1);
}
len
}

/// If the struct has any fields.
/// If the struct has any fields, excluding the discriminator.
fn has_fields(&self) -> bool {
self.len_fields() > 0
}

/// // If the struct has any fields, excluding the discriminator.
fn has_fields_without_descriminator(&self) -> bool {
self.len_fields_without_descriminator() > 0
}

fn array_items(&self) -> Result<&ReferenceOr<Schema>> {
get_schema_array_items(&self.schema.common)
}
Expand Down Expand Up @@ -513,8 +503,8 @@ pub fn create_models(cg: &mut CodeGen) -> Result<ModelsCode> {
let mut schema = schema.clone();
let tag_property = schema.properties.iter().find(|property| property.name() == tag);
let tag_property_description = tag_property.and_then(|property| property.schema().schema.common.description.clone());
schema.properties.retain(|property| property.name() != tag);
if schema.has_fields() {
schema.properties.retain(|property| property.name() != tag);
models.push(ModelCode::Struct(create_struct(
cg,
&schema,
Expand Down Expand Up @@ -931,7 +921,7 @@ fn create_struct(

for base_schema in schema.all_of() {
// skip empty base types
if !base_schema.has_fields_without_descriminator() {
if !base_schema.has_fields() {
continue;
}
let schema_name = base_schema.name()?;
Expand Down

0 comments on commit 060ad1a

Please sign in to comment.