Skip to content

Commit

Permalink
Fix respect required attribute (#863)
Browse files Browse the repository at this point in the history
* Fix respect required attribute

* wip
  • Loading branch information
chrislearn authored Aug 13, 2024
1 parent 5b3bfaa commit 050fbbb
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 33 deletions.
23 changes: 11 additions & 12 deletions crates/oapi-macros/src/parameter/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use syn::{
use crate::component::{self, ComponentSchema};
use crate::doc_comment::CommentAttributes;
use crate::feature::{
self, impl_into_inner, impl_merge, parse_features, pop_feature, pop_feature_as_inner, AdditionalProperties,
AllowReserved, DefaultStyle, Example, ExclusiveMaximum, ExclusiveMinimum, Explode, Feature, FeaturesExt, Format,
Inline, MaxItems, MaxLength, Maximum, Merge, MinItems, MinLength, Minimum, MultipleOf, Nullable, Pattern, ReadOnly,
Rename, RenameAll, SchemaWith, Style, ToParametersNames, TryToTokensExt, WriteOnly, XmlAttr,
self, impl_into_inner, impl_merge, parse_features, pop_feature, AdditionalProperties, AllowReserved, DefaultStyle,
Example, ExclusiveMaximum, ExclusiveMinimum, Explode, Feature, FeaturesExt, Format, Inline, MaxItems, MaxLength,
Maximum, Merge, MinItems, MinLength, Minimum, MultipleOf, Nullable, Pattern, ReadOnly, Rename, RenameAll,
SchemaWith, Style, ToParametersNames, TryToTokensExt, WriteOnly, XmlAttr,
};
use crate::parameter::ParameterIn;
use crate::serde_util::{self, RenameRule, SerdeContainer, SerdeValue};
Expand Down Expand Up @@ -568,15 +568,14 @@ impl TryToTokens for Parameter<'_> {
.transpose()?
.unwrap_or(type_tree);

let required = pop_feature_as_inner!(param_features => Feature::Required(_v))
.as_ref()
.map(crate::feature::Required::is_true)
.unwrap_or(false);

let non_required = (component.is_option() && !required)
|| !crate::is_required(self.field_serde_params.as_ref(), self.serde_container);
let required: Required = (!non_required).into();
let required: Option<feature::Required> = pop_feature!(param_features => Feature::Required(_)).into_inner();
let component_required =
!component.is_option() && crate::is_required(self.field_serde_params.as_ref(), self.serde_container);

let required = match (required, component_required) {
(Some(required_feature), _) => Into::<Required>::into(required_feature.is_true()),
(None, component_required) => Into::<Required>::into(component_required),
};
tokens.extend(quote! {
.required(#required)
});
Expand Down
13 changes: 7 additions & 6 deletions crates/oapi-macros/src/schema/struct_schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,13 @@ impl TryToTokens for NamedStructSchema<'_> {
.property(#name, #property)
});

if (!is_option && crate::is_required(field_rule.as_ref(), container_rules.as_ref()))
|| required
.as_ref()
.map(crate::feature::Required::is_true)
.unwrap_or(false)
{
let component_required = !is_option && crate::is_required(field_rule.as_ref(), container_rules.as_ref());
let required = match (required, component_required) {
(Some(required), _) => required.is_true(),
(None, component_required) => component_required,
};

if required {
object_tokens.extend(quote! {
.required(#name)
})
Expand Down
3 changes: 0 additions & 3 deletions crates/oapi/src/openapi/schema/all_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ impl AllOf {
}

/// Add or change example shown in UI of the value for richer documentation.
///
/// **Deprecated since 3.0.x. Prefer [`AllOf::examples`] instead**
#[deprecated = "Since OpenAPI 3.1 prefer using `examples`"]
pub fn example<V: Into<Value>>(mut self, example: V) -> Self {
self.examples.push(example.into());
self
Expand Down
3 changes: 0 additions & 3 deletions crates/oapi/src/openapi/schema/any_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ impl AnyOf {
}

/// Add or change example shown in UI of the value for richer documentation.
///
/// **Deprecated since 3.0.x. Prefer [`AnyOf::examples`] instead**
#[deprecated = "Since OpenAPI 3.1 prefer using `examples`"]
pub fn example<V: Into<Value>>(mut self, example: V) -> Self {
self.examples.push(example.into());
self
Expand Down
3 changes: 0 additions & 3 deletions crates/oapi/src/openapi/schema/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ impl Array {
}

/// Add or change example shown in UI of the value for richer documentation.
///
/// **Deprecated since 3.0.x. Prefer [`Array::examples`] instead**
#[deprecated = "Since OpenAPI 3.1 prefer using `examples`"]
pub fn example<V: Into<Value>>(mut self, example: V) -> Self {
self.examples.push(example.into());
self
Expand Down
3 changes: 0 additions & 3 deletions crates/oapi/src/openapi/schema/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,6 @@ impl Object {
}

/// Add or change example shown in UI of the value for richer documentation.
///
/// **Deprecated since 3.0.x. Prefer [`Object::examples`] instead**
#[deprecated = "Since OpenAPI 3.1 prefer using `examples`"]
pub fn example<V: Into<Value>>(mut self, example: V) -> Self {
self.examples.push(example.into());
self
Expand Down
3 changes: 0 additions & 3 deletions crates/oapi/src/openapi/schema/one_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ impl OneOf {
}

/// Add or change example shown in UI of the value for richer documentation.
///
/// **Deprecated since 3.0.x. Prefer [`OneOf::examples`] instead**
#[deprecated = "Since OpenAPI 3.1 prefer using `examples`"]
pub fn example<V: Into<Value>>(mut self, example: V) -> Self {
self.examples.push(example.into());
self
Expand Down

0 comments on commit 050fbbb

Please sign in to comment.