-
-
Notifications
You must be signed in to change notification settings - Fork 325
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
Invalid schema generated for #[serde(flatten)] HashMap<String, serde_json::Value>
#844
Comments
#[serde(flatten)] HashMap<String, serde_json::Value>
Ah, thanks for this. Super helpful. So, basically:
This sounds to me like it's worth raising an issue against At the very least the proposed solution won't work when users embed legit (non-flattened) I can see there was one fix for flattening in the last schemars release, but it looks unrelated, and don't see any other related flattening issues there. |
It also makes sense that it doesn't happen for |
Btw, what schema do you get if you instead use the following struct: pub struct TestSpec {
foo: String,
#[serde(flatten)]
arbitrary: serde_json::Value,
} i don't think you need to nest the |
When replacing the properties:
spec:
properties:
foo:
type: string
required:
- foo
type: object
required:
- spec
title: Test
type: object But with that, I'm lost in several ways: If I try to just create a resource "normally", with
That's about to be expected, kubernetes doesn't like properties that aren't explicitly allowed. (So for me, just using But it works with
So, it just skips validation, but then drops the properties. Is (Nevermind that I tried spawning kubectl from rust, which for some reason implicitly specifies |
You could try to specify |
Is… is there a way to specify |
Yeah, should possible by customizing parts of the schema or the entire schema. There is an example of that in i haven't gotten a specific example with |
Okay, this took me a good long 1½ hours to figure out (I had mistakenly thought this would be easy…): In case of a So this is a passable workaround (for both pub struct TestSpec {
foo: String,
#[serde(flatten)]
#[schemars(schema_with = "preserve_arbitrary")]
arbitrary: HashMap<String, serde_json::Value>,
}
fn preserve_arbitrary(_gen: &mut schemars::gen::SchemaGenerator) -> Schema {
let mut obj = SchemaObject::default();
obj.extensions
.insert("x-kubernetes-preserve-unknown-fields".into(), true.into());
Schema::Object(obj)
} So for the real fix, what kind of issue do you want to raise against |
Hm, yeah, we are doing something specifically for structural schemas, but that's a kubernetes specific restriction. I feel like there's probably a good case for Still, I am not sure we need to jump through these hoops (even if schemars could be improved here). If you only use a flattenend pub struct TestSpec {
foo: String,
#[serde(flatten)]
#[schemars(schema_with = "preserve_arbitrary")]
arbitrary: serde_json::Value,
} you do get the following schema: openAPIV3Schema:
description: "Auto-generated derived type for TestSpec via `CustomResource`"
properties:
spec:
properties:
foo:
type: string
required:
- foo
type: object
x-kubernetes-preserve-unknown-fields: true
required:
- spec
title: Test
type: object which i think is an easier way to describe what you want? |
(The |
…o serde(flatten) See also: kube-rs#844
…o serde(flatten) See also: kube-rs#844
…o serde(flatten) See also: kube-rs#844 Signed-off-by: Julius Michaelis <gitter@liftm.de>
* Fix schemas containing both properties and additionalProperties due to serde(flatten) See also: #844 Signed-off-by: Julius Michaelis <gitter@liftm.de> * comment suggestion Co-authored-by: Eirik A <sszynrae@gmail.com> Co-authored-by: Eirik A <sszynrae@gmail.com>
Thanks for your efforts. I don't know if you want to keep this open for the |
Current and expected behavior
The following CRDT
derives an openAPIV3Schema that contains a property for foo, and additionalProperties.
Trouble is that k8s has some extra rules over JSON schemas, the problem in this case is:
So the generated schema is invalid.
Possible solution
It may be possible to somehow configure
schemars
to generate the wanted schema, but I haven't looked deeply intoschemars
'GenVisitor
.As a workaround, one can convert the generated schema to a
serde_json::Value
and manually replace all"additionalProperties": true
by"x-kubernetes-preserve-unknown-fields": true
. Code example. But that's maybe to ugly a hack for a PR?Additional context
(Oddly, this doesn't happen with
#[serde(flatten)] arbitrary: HashMap<String, String>
, for which nothing at all is generated, which also seems wrong.)Environment
Configuration and features
Affected crates
kube-derive
Would you like to work on fixing this bug?
maybe
The text was updated successfully, but these errors were encountered: