Repository navigation
[rust-server] Validate model-valued wrapper fields #25262
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
Open
EricLiu2000
wants to merge
1
commit into
OpenAPITools:master
Choose a base branch
from
EricLiu2000:codex/recursive-model-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
modules/openapi-generator/src/test/resources/3_0/rust-server/recursive-validation.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Generate recursive-validation.yaml with --generate-alias-as-model and | ||
| // --additional-properties packageName=recursive_validation. Copy this file to | ||
| // tests/recursive_validation.rs in the output, then run | ||
| // cargo test --no-default-features --features validate --test recursive_validation. | ||
| #![cfg(feature = "validate")] | ||
| use recursive_validation::models::{ | ||
| BoundedChildren, MixedChoice, ModelAnyOf, ModelChoice, NamedModelChoice, NullableChildren, | ||
| NullableStringItems, Parent, Positive, PositiveMap, Positives, PrimitiveChoice, StringAlias, | ||
| StringItems, StringValue, | ||
| }; | ||
| use serde_valid::Validate; | ||
|
|
||
| #[test] | ||
| fn root_validation_reaches_array_wrapper_items_and_bounds() { | ||
| for values in [vec![], vec![1], vec![1, 1], vec![1, 1, 1], vec![0]] { | ||
| let expected = (1..=2).contains(&values.len()) && values.iter().all(|value| *value >= 1); | ||
| let root = Parent { | ||
| children: Positives::from( | ||
| values | ||
| .into_iter() | ||
| .map(|value| Positive { value }) | ||
| .collect::<Vec<_>>(), | ||
| ), | ||
| }; | ||
| assert_eq!(root.validate().is_ok(), expected); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn bounded_fields_validate_their_size_and_children() { | ||
| for values in [vec![], vec![1], vec![1, 1], vec![1, 1, 1], vec![0]] { | ||
| for optional in [false, true] { | ||
| let expected = | ||
| (1..=2).contains(&values.len()) && values.iter().all(|value| *value >= 1); | ||
| let children = values | ||
| .iter() | ||
| .map(|value| Positive { value: *value }) | ||
| .collect(); | ||
| let root = if optional { | ||
| BoundedChildren { | ||
| children: vec![Positive { value: 1 }], | ||
| optional_children: Some(children), | ||
| } | ||
| } else { | ||
| BoundedChildren { | ||
| children, | ||
| optional_children: None, | ||
| } | ||
| }; | ||
| assert_eq!(root.validate().is_ok(), expected); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn primitive_array_wrappers_validate_their_size() { | ||
| for size in 0..=3 { | ||
| let value = StringItems::from(vec![String::new(); size]); | ||
| assert_eq!(value.validate().is_ok(), (1..=2).contains(&size)); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn map_wrappers_validate_their_values() { | ||
| for value in [0, 1] { | ||
| let root = PositiveMap::from(std::collections::HashMap::from([( | ||
| "key".to_owned(), | ||
| Positive { value }, | ||
| )])); | ||
| assert_eq!(root.validate().is_ok(), value == 1); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn reference_wrappers_validate_their_inner_model() { | ||
| for value in ["", "x"] { | ||
| let root = StringAlias::from(StringValue::from(value.to_owned())); | ||
| assert_eq!(root.validate().is_ok(), !value.is_empty()); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn union_wrappers_compile_with_model_and_primitive_members() { | ||
| let model = ModelChoice::from(swagger::OneOf2::A(Positive { value: 1 })); | ||
| let named = NamedModelChoice::from(swagger::OneOf2::A(Positive { value: 1 })); | ||
| let any = ModelAnyOf::from(swagger::AnyOf2::B(Positives::from(vec![Positive { | ||
| value: 1, | ||
| }]))); | ||
| let mixed = MixedChoice::from(swagger::OneOf2::B("future".to_owned())); | ||
| let primitive: PrimitiveChoice = serde_json::from_str("42").unwrap(); | ||
| assert!(model.validate().is_ok()); | ||
| assert!(named.validate().is_ok()); | ||
| assert!(any.validate().is_ok()); | ||
| assert!(mixed.validate().is_ok()); | ||
| assert!(primitive.validate().is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn bounded_nullable_fields_keep_compiling_and_validate_present_models() { | ||
| let value = NullableChildren { | ||
| children: swagger::Nullable::Null, | ||
| optional_children: None, | ||
| }; | ||
| assert!(value.validate().is_ok()); | ||
| for optional in [false, true] { | ||
| for value in [0, 1] { | ||
| let children = swagger::Nullable::Present(vec![Positive { value }]); | ||
| let root = if optional { | ||
| NullableChildren { | ||
| children: swagger::Nullable::Null, | ||
| optional_children: Some(children), | ||
| } | ||
| } else { | ||
| NullableChildren { | ||
| children, | ||
| optional_children: Some(swagger::Nullable::Null), | ||
| } | ||
| }; | ||
| assert_eq!(root.validate().is_ok(), value == 1); | ||
| } | ||
| } | ||
| let items = NullableStringItems::from(vec![String::new()]); | ||
| assert!(items.validate().is_ok()); | ||
| } |
106 changes: 106 additions & 0 deletions
106
modules/openapi-generator/src/test/resources/3_0/rust-server/recursive-validation.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| openapi: 3.0.3 | ||
| info: {title: Recursive validation, version: 1.0.0} | ||
| paths: | ||
| /parents/{name}: | ||
| get: | ||
| operationId: getParent | ||
| parameters: | ||
| - name: name | ||
| in: path | ||
| required: true | ||
| schema: {type: string, pattern: '^x+$'} | ||
| responses: | ||
| '200': | ||
| description: A parent | ||
| content: | ||
| application/json: | ||
| schema: {$ref: '#/components/schemas/Parent'} | ||
| components: | ||
| schemas: | ||
| Positive: | ||
| type: object | ||
| required: [value] | ||
| properties: | ||
| value: {type: integer, minimum: 1} | ||
| Positives: | ||
| type: array | ||
| minItems: 1 | ||
| maxItems: 2 | ||
| items: {$ref: '#/components/schemas/Positive'} | ||
| Parent: | ||
| type: object | ||
| required: [children] | ||
| properties: | ||
| children: {$ref: '#/components/schemas/Positives'} | ||
| ModelChoice: | ||
| oneOf: | ||
| - $ref: '#/components/schemas/Positive' | ||
| - $ref: '#/components/schemas/Parent' | ||
| NamedModelChoice: | ||
| x-one-of-name: 'swagger::OneOf2<Positive,Parent>' | ||
| oneOf: | ||
| - $ref: '#/components/schemas/Positive' | ||
| - $ref: '#/components/schemas/Parent' | ||
| MixedChoice: | ||
| oneOf: | ||
| - $ref: '#/components/schemas/Positive' | ||
| - type: string | ||
| PrimitiveChoice: | ||
| anyOf: | ||
| - type: integer | ||
| - type: string | ||
| ModelAnyOf: | ||
| anyOf: | ||
| - $ref: '#/components/schemas/Positive' | ||
| - $ref: '#/components/schemas/Positives' | ||
| BoundedChildren: | ||
| type: object | ||
| required: [children] | ||
| properties: | ||
| children: | ||
| type: array | ||
| minItems: 1 | ||
| maxItems: 2 | ||
| items: | ||
| $ref: '#/components/schemas/Positive' | ||
| optionalChildren: | ||
| type: array | ||
| minItems: 1 | ||
| maxItems: 2 | ||
| items: | ||
| $ref: '#/components/schemas/Positive' | ||
| StringItems: | ||
| type: array | ||
| minItems: 1 | ||
| maxItems: 2 | ||
| items: {type: string} | ||
| PositiveMap: | ||
| type: object | ||
| additionalProperties: {$ref: '#/components/schemas/Positive'} | ||
| StringValue: | ||
| type: string | ||
| minLength: 1 | ||
| StringAlias: | ||
| $ref: '#/components/schemas/StringValue' | ||
| NullableChildren: | ||
| type: object | ||
| required: [children] | ||
| properties: | ||
| children: | ||
| type: array | ||
| nullable: true | ||
| minItems: 1 | ||
| maxItems: 2 | ||
| items: {$ref: '#/components/schemas/Positive'} | ||
| optionalChildren: | ||
| type: array | ||
| nullable: true | ||
| minItems: 1 | ||
| maxItems: 2 | ||
| items: {$ref: '#/components/schemas/Positive'} | ||
| NullableStringItems: | ||
| type: array | ||
| nullable: true | ||
| minItems: 1 | ||
| maxItems: 2 | ||
| items: {type: string} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.