openconfig / ygot

A YANG-centric Go toolkit - Go/Protobuf Code Generation; Validation; Marshaling/Unmarshaling
Apache License 2.0
284 stars 106 forks source link

ytypes.Unmarshal() treating 50 as a float64 even though I am converting 50 to string before unmarshalling #972

Open ruchakulkarni11 opened 4 months ago

ruchakulkarni11 commented 4 months ago

I have an enum with index 1 and value 50. schema required to convert the enum index to enum value (string). I am converting 50 to string type but while ytypes.Unmarshal() sanitizeJson method is treating 50 as a float64 even though I am converting 50 to string before unmarshalling.

Is there any way I can convert the type of 50 to string?

I would appreciate any inputs!

wenovus commented 4 months ago

Enums are marshalled as strings per RFC7951: https://datatracker.ietf.org/doc/html/rfc7951#section-6.4

ruchakulkarni11 commented 4 months ago

yes, thats right but my string value is 50, and failing while unmarshalling.

wenovus commented 4 months ago

Can you provide your YANG schema as well as a reproducing example? I have to go for today but I can take a look with those information when I get a chance.

I'm not quite sure what you mean by the string value is 50. This means the JSON needs to be "50" rather than 50. I'm not immediately seeing how sanitizeJSON could be the issue in any case assuming the leaf is an enum.

ruchakulkarni11 commented 4 months ago

Hello, Sorry for late reply.

Suppose enum is defined as follow,

typedef example_enum {
    type enumeration {
        enum 25 { value 1; }
        enum 50 { value 2; }
        enum 75 { value 3; }
        enum 100 { value 4; }
    }
}

Now here when enum index 2 is converted to name, its 50. Here enum name is string so 50 must be of type string, but the ytypes.Unmarshal returns error as "got float64 type for field, expect string"

robshakir commented 3 months ago

Hi,

Could you provide the JSON input you're using? This sounds like you might have this field set as foo: 50 rather than foo: "50".

Thanks, r.

ruchakulkarni11 commented 2 months ago

If I set foo: "50", unmarshalling fails because "50" does not match with the enum-name 50.

ytypes.Unmarshal: "50" is not a valid value for enum field Id

robshakir commented 1 month ago

Can you provide both the JSON and the YANG module? It's important how the leaf is defined to be able to debug this.