-
Notifications
You must be signed in to change notification settings - Fork 115
Fix invalid Rust emitted for defaults on types with flattened properties #1039
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
danieleades
wants to merge
1
commit into
oxidecomputer:main
Choose a base branch
from
danieleades:fix/flatten-default-ident
base: main
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.
+251
−2
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "$comment": "regression test: a default value for a struct type with a flattened property must produce a valid Rust identifier for the flattened field", | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "title": "Container", | ||
| "type": "object", | ||
| "properties": { | ||
| "thing": { | ||
| "type": "object", | ||
| "properties": { | ||
| "id": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "additionalProperties": { | ||
| "type": "string" | ||
| }, | ||
| "default": { | ||
| "id": "abc", | ||
| "extra_key": "extra_value" | ||
| } | ||
| } | ||
| } | ||
| } |
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,226 @@ | ||
| #![deny(warnings)] | ||
| #[doc = r" Error types."] | ||
| pub mod error { | ||
| #[doc = r" Error from a `TryFrom` or `FromStr` implementation."] | ||
| pub struct ConversionError(::std::borrow::Cow<'static, str>); | ||
| impl ::std::error::Error for ConversionError {} | ||
| impl ::std::fmt::Display for ConversionError { | ||
| fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { | ||
| ::std::fmt::Display::fmt(&self.0, f) | ||
| } | ||
| } | ||
| impl ::std::fmt::Debug for ConversionError { | ||
| fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { | ||
| ::std::fmt::Debug::fmt(&self.0, f) | ||
| } | ||
| } | ||
| impl From<&'static str> for ConversionError { | ||
| fn from(value: &'static str) -> Self { | ||
| Self(value.into()) | ||
| } | ||
| } | ||
| impl From<String> for ConversionError { | ||
| fn from(value: String) -> Self { | ||
| Self(value.into()) | ||
| } | ||
| } | ||
| } | ||
| #[doc = "`Container`"] | ||
| #[doc = r""] | ||
| #[doc = r" <details><summary>JSON schema</summary>"] | ||
| #[doc = r""] | ||
| #[doc = r" ```json"] | ||
| #[doc = "{"] | ||
| #[doc = " \"title\": \"Container\","] | ||
| #[doc = " \"type\": \"object\","] | ||
| #[doc = " \"properties\": {"] | ||
| #[doc = " \"thing\": {"] | ||
| #[doc = " \"default\": {"] | ||
| #[doc = " \"extra_key\": \"extra_value\","] | ||
| #[doc = " \"id\": \"abc\""] | ||
| #[doc = " },"] | ||
| #[doc = " \"type\": \"object\","] | ||
| #[doc = " \"properties\": {"] | ||
| #[doc = " \"id\": {"] | ||
| #[doc = " \"type\": \"string\""] | ||
| #[doc = " }"] | ||
| #[doc = " },"] | ||
| #[doc = " \"additionalProperties\": {"] | ||
| #[doc = " \"type\": \"string\""] | ||
| #[doc = " }"] | ||
| #[doc = " }"] | ||
| #[doc = " },"] | ||
| #[doc = " \"$comment\": \"regression test: a default value for a struct type with a flattened property must produce a valid Rust identifier for the flattened field\""] | ||
| #[doc = "}"] | ||
| #[doc = r" ```"] | ||
| #[doc = r" </details>"] | ||
| #[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] | ||
| pub struct Container { | ||
| #[serde(default, skip_serializing_if = "::std::option::Option::is_none")] | ||
| pub thing: ::std::option::Option<ContainerThing>, | ||
| } | ||
| impl ::std::default::Default for Container { | ||
| fn default() -> Self { | ||
| Self { | ||
| thing: Default::default(), | ||
| } | ||
| } | ||
| } | ||
| impl Container { | ||
| pub fn builder() -> builder::Container { | ||
| Default::default() | ||
| } | ||
| } | ||
| #[doc = "`ContainerThing`"] | ||
| #[doc = r""] | ||
| #[doc = r" <details><summary>JSON schema</summary>"] | ||
| #[doc = r""] | ||
| #[doc = r" ```json"] | ||
| #[doc = "{"] | ||
| #[doc = " \"default\": {"] | ||
| #[doc = " \"extra_key\": \"extra_value\","] | ||
| #[doc = " \"id\": \"abc\""] | ||
| #[doc = " },"] | ||
| #[doc = " \"type\": \"object\","] | ||
| #[doc = " \"properties\": {"] | ||
| #[doc = " \"id\": {"] | ||
| #[doc = " \"type\": \"string\""] | ||
| #[doc = " }"] | ||
| #[doc = " },"] | ||
| #[doc = " \"additionalProperties\": {"] | ||
| #[doc = " \"type\": \"string\""] | ||
| #[doc = " }"] | ||
| #[doc = "}"] | ||
| #[doc = r" ```"] | ||
| #[doc = r" </details>"] | ||
| #[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] | ||
| pub struct ContainerThing { | ||
| #[serde(default, skip_serializing_if = "::std::option::Option::is_none")] | ||
| pub id: ::std::option::Option<::std::string::String>, | ||
| #[serde(flatten)] | ||
| pub extra: ::std::collections::HashMap<::std::string::String, ::std::string::String>, | ||
| } | ||
| impl ::std::default::Default for ContainerThing { | ||
| fn default() -> Self { | ||
| ContainerThing { | ||
| id: ::std::option::Option::Some("abc".to_string()), | ||
| extra: [("extra_key".to_string(), "extra_value".to_string())] | ||
| .into_iter() | ||
| .collect(), | ||
| } | ||
| } | ||
| } | ||
| impl ContainerThing { | ||
| pub fn builder() -> builder::ContainerThing { | ||
| Default::default() | ||
| } | ||
| } | ||
| #[doc = r" Types for composing complex structures."] | ||
| pub mod builder { | ||
| #[derive(Clone, Debug)] | ||
| pub struct Container { | ||
| thing: ::std::result::Result< | ||
| ::std::option::Option<super::ContainerThing>, | ||
| ::std::string::String, | ||
| >, | ||
| } | ||
| impl ::std::default::Default for Container { | ||
| fn default() -> Self { | ||
| Self { | ||
| thing: Ok(Default::default()), | ||
| } | ||
| } | ||
| } | ||
| impl Container { | ||
| pub fn thing<T>(mut self, value: T) -> Self | ||
| where | ||
| T: ::std::convert::TryInto<::std::option::Option<super::ContainerThing>>, | ||
| T::Error: ::std::fmt::Display, | ||
| { | ||
| self.thing = value | ||
| .try_into() | ||
| .map_err(|e| format!("error converting supplied value for thing: {e}")); | ||
| self | ||
| } | ||
| } | ||
| impl ::std::convert::TryFrom<Container> for super::Container { | ||
| type Error = super::error::ConversionError; | ||
| fn try_from( | ||
| value: Container, | ||
| ) -> ::std::result::Result<Self, super::error::ConversionError> { | ||
| Ok(Self { | ||
| thing: value.thing?, | ||
| }) | ||
| } | ||
| } | ||
| impl ::std::convert::From<super::Container> for Container { | ||
| fn from(value: super::Container) -> Self { | ||
| Self { | ||
| thing: Ok(value.thing), | ||
| } | ||
| } | ||
| } | ||
| #[derive(Clone, Debug)] | ||
| pub struct ContainerThing { | ||
| id: ::std::result::Result< | ||
| ::std::option::Option<::std::string::String>, | ||
| ::std::string::String, | ||
| >, | ||
| extra: ::std::result::Result< | ||
| ::std::collections::HashMap<::std::string::String, ::std::string::String>, | ||
| ::std::string::String, | ||
| >, | ||
| } | ||
| impl ::std::default::Default for ContainerThing { | ||
| fn default() -> Self { | ||
| Self { | ||
| id: Ok(Default::default()), | ||
| extra: Err("no value supplied for extra".to_string()), | ||
| } | ||
| } | ||
| } | ||
| impl ContainerThing { | ||
| pub fn id<T>(mut self, value: T) -> Self | ||
| where | ||
| T: ::std::convert::TryInto<::std::option::Option<::std::string::String>>, | ||
| T::Error: ::std::fmt::Display, | ||
| { | ||
| self.id = value | ||
| .try_into() | ||
| .map_err(|e| format!("error converting supplied value for id: {e}")); | ||
| self | ||
| } | ||
| pub fn extra<T>(mut self, value: T) -> Self | ||
| where | ||
| T: ::std::convert::TryInto< | ||
| ::std::collections::HashMap<::std::string::String, ::std::string::String>, | ||
| >, | ||
| T::Error: ::std::fmt::Display, | ||
| { | ||
| self.extra = value | ||
| .try_into() | ||
| .map_err(|e| format!("error converting supplied value for extra: {e}")); | ||
| self | ||
| } | ||
| } | ||
| impl ::std::convert::TryFrom<ContainerThing> for super::ContainerThing { | ||
| type Error = super::error::ConversionError; | ||
| fn try_from( | ||
| value: ContainerThing, | ||
| ) -> ::std::result::Result<Self, super::error::ConversionError> { | ||
| Ok(Self { | ||
| id: value.id?, | ||
| extra: value.extra?, | ||
| }) | ||
| } | ||
| } | ||
| impl ::std::convert::From<super::ContainerThing> for ContainerThing { | ||
| fn from(value: super::ContainerThing) -> Self { | ||
| Self { | ||
| id: Ok(value.id), | ||
| extra: Ok(value.extra), | ||
| } | ||
| } | ||
| } | ||
| } | ||
| fn main() {} | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the key to the map should actually be a newtype wrapper around a String that prohibits values that conflict with existing property names (here "id")...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think that is a separate correctness gap in how we model typed
additionalProperties. The generated public map lets callers insertid, which can produce a duplicate or conflicting key during serialization even though deserialization routesidto the declared field. A key newtype or custom map wrapper that excludes declared property names would make that invariant explicit.I would keep this PR scoped to the invalid Rust emission, since it is already a complete compile fix, and track the key-collision behavior separately with construction and round-trip tests.