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

oapi: Fix additionalProperties not working for Object #522

Merged
merged 6 commits into from
Nov 29, 2023
Merged
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
20 changes: 17 additions & 3 deletions crates/oapi-macros/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use quote::{quote, quote_spanned, ToTokens};
use syn::spanned::Spanned;

use crate::doc_comment::CommentAttributes;
use crate::feature::{pop_feature, Feature, FeaturesExt, IsInline, Minimum, Nullable, ToTokensExt, Validatable};
use crate::feature::{
pop_feature, AdditionalProperties, Feature, FeaturesExt, IsInline, Minimum, Nullable, ToTokensExt, Validatable,
};
use crate::schema_type::{SchemaFormat, SchemaType};
use crate::type_tree::{GenericType, TypeTree, ValueType};
use crate::Deprecated;
Expand Down Expand Up @@ -336,10 +338,22 @@ impl<'c> ComponentSchema {
let is_inline = features.is_inline();

if type_tree.is_object() {
let oapi = crate::oapi_crate();
let example = features.pop_by(|feature| matches!(feature, Feature::Example(_)));
let additional_properties = pop_feature!(features => Feature::AdditionalProperties(_))
.unwrap_or_else(|| Feature::AdditionalProperties(AdditionalProperties(true)));
let nullable = pop_feature!(features => Feature::Nullable(_));
let default = pop_feature!(features => Feature::Default(_));

tokens.extend(quote! {
#oapi::oapi::Object::new()
#description_stream #deprecated_stream #nullable
})
#additional_properties
#description_stream
#deprecated_stream
#default
});
example.to_tokens(tokens);
nullable.to_tokens(tokens)
} else {
let type_path = &**type_tree.path.as_ref().unwrap();
let schema = if type_definition {
Expand Down
6 changes: 3 additions & 3 deletions crates/oapi-macros/src/schema/struct_schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct NamedStructFieldOptions<'a> {
}

impl NamedStructSchema<'_> {
fn field_to_schema_property<R>(
fn field_as_schema_property<R>(
&self,
field: &Field,
flatten: bool,
Expand Down Expand Up @@ -144,7 +144,7 @@ impl ToTokens for NamedStructSchema<'_> {
field_name = &field_name[2..];
}

self.field_to_schema_property(
self.field_as_schema_property(
field,
false,
&container_rules,
Expand Down Expand Up @@ -201,7 +201,7 @@ impl ToTokens for NamedStructSchema<'_> {
let mut flattened_map_field = None;

for field in flatten_fields {
self.field_to_schema_property(
self.field_as_schema_property(
field,
true,
&container_rules,
Expand Down