-
Notifications
You must be signed in to change notification settings - Fork 223
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
[Question] Mismatched default value type handling #3398
Comments
Thanks for starting the conversation on this! We previously got feedback that being "too harsh during validation and blocking generation early on" is not appreciated but customers (I personally prefer failing as early as possible), so I don't think changing the severity is a viable option. Int32 doesn't have an infinite representation in most languages. (double often does) I don't think diving into specification (IEEE734) of numeric formats and how well they were implemented in different languages as well as how well they are being used by APIs is going to be a valuable use of our time. It's going to be a whack a mole game. What I'm going to suggest instead is that for known types (int, double, etc...) we use the corresponding type TryParse method before setting the default value in the DOM in kiota builder. This way we can check the value is supported for a limited number of types, and ignore other default values when they don't pass the parse test. Thoughts? |
I do agree with you, but I have received this feedback as well, and can relate 🙂 .
I haven't expressed it, but I was thinking at falling back to
I understand the reasons for you to propose this, but I'm not 100% convinced, let me try to elaborate: Probably, the most "semantically correct" behavior would be to defer the computation of the Thinking about the issue twice, the resolution we are discussing only applies to(some of) the primitives. Should we expand the scope to |
inf and maxvalue mean two very different things though. I don't think mapping one to another would help the users here.
Besides the validation logic not being tied to CSharp when the target language is not CSharp, thus avoiding "CSharp assumptions that don't hold in other languages", I'm curious of what other advantages do you see here?
I'm not sure what a default object representation looks like according to OAS? do you have examples? would that be tied to a specific serialization format? |
You are completely right, I reported the issue upstream.
For example, we wouldn't need to re-implement the logic in two different places(generation and runtime) when dealing with more complex types (more below on this subject).
I haven't realized I was about to open a 🕳️ 🐇 here 😅 .
I found some more context into this issue. This means that an OAS like this: openapi: 3.0.0
info:
title: Test
version: 1.0.0
description: something
paths:
/api/something/v1:
post:
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/Test"
responses:
"200":
description: "something"
content:
application/json: {}
components:
schemas:
Test:
description: this is a first test
properties:
test:
description: a test
type: object
properties:
foo:
type: string
bar:
type: integer
default:
test:
foo: "baz"
bar: 42
type: object is valid and is expected to use the
At this point, we can separate this issue into 2:
We are already discussing possible solutions for the former, to tackle the latter, in another generator, I decided to defer the evaluation to the runtime leveraging the available deserializer, here you have an example of what it looks like. Please note that the "deserializer approach" can be optimized for primitive values (and I do it ). |
I agree, let's leave default values for objects/arrays on the side for now. There's no demand for it, it's highly coupled with the serialization format, and I think it's an edge case (defining a whole object worth of default vs defining defaults for each of the scalar properties). Refocusing the discussion on the scalar values, I'm guessing you're suggesting to emit something like that? public class Test {
public int Foo;
public Test() {
Foo = ParseNodeFactoryRegistry.DefaultInstance.GetRootParseNode("application/json", new MemoryStream(UTF8.GetBytes("100"))).GetIntValue();
}
} It feels like a lot of ceremony even though we'd enable a consistent behaviour between default values and deserialization. Also how do we know to use application/json? |
I differ on this one, let me explain some reasons: On the project's landing page, there is written (and I always promote Kiota in my talks this way):
I do believe that
Yes, the snippet resembles the essence of my proposal.
That's correct, but it's the most "generic" approach that can be used to implement full support.
This comes from the spec:
|
All very valid points. public class Test {
public int Foo;
public Test() {
Foo = ParseNodeFactoryRegistry.GetRootParseNode("application/json", "100").GetIntValue();
}
} This solution is a bit tied to JSON representations since the default notion is defined in JSON Schema. (serialization formats that are binary, like cbor, or that use ident as meaning like yaml will break with this approach). |
I don't thins this should ever go stale, we are just waiting for a plan to shape up. |
update: since #3406 has been implemented, we have a way forward:
The only gap left is probably enums, but that's already handled today. Is this something you'd like to take a stab at? |
I'm not sure I have bandwidth for this ATM 😞 , if no one is picking it up, I probably will but it's gonna take some time. |
Description
When the default value of a field doesn't match the type a warning is emitted by
kiota
, but, in this case, the generated code is not going to compile forallOf
the non-dynamic languages.Original issue
Faced this issue here in the OpenAI spec.
Reproducer
A minimal spec like this:
This would produce the following Warning:
But it generates code that is not going to compile:
Desired resolution
I think that we can improve over the current situation, as having code generated but that is going to fail to compile is suboptimal.
There are a few directions we can consider:
Int32.Parse("inf")
so that the error will be moved at the user's runtime. Possibly throwing the relevant exception only in case thedefault
needs to get applied.Happy to hear more feedback!
The text was updated successfully, but these errors were encountered: