Skip to content
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

jx.DecodeStr passed unquoted string value and fails #1173

Open
adrianduke opened this issue Feb 7, 2024 · 0 comments
Open

jx.DecodeStr passed unquoted string value and fails #1173

adrianduke opened this issue Feb 7, 2024 · 0 comments

Comments

@adrianduke
Copy link

I've got a situation where I'm getting the following response:

{"error_message":"operation InstantiateRelationshipSchema: decode request: decode \"vertex_a\": unexpected json type \"invalid\""}

The schema in question looks like the following:

InstantiateRelationshipSchema:
  allOf:
    - $ref: '#/RelatiesSchema'
    - type: object
      required:
        - "name"
      properties:
        name:
          type: string
          description: Name of the schema
          minLength: 1
          example: isMarriedTo
          
RelatiesSchema:
  type: object
  required:
    - vertex_a
    - vertex_b
  properties:
    vertex_a:
      oneOf:
        - type: string
          example: 8877b096617d7ab3a645243fe985
        - $ref: '#/EntitySchema'
    vertex_b:
      oneOf:
        - type: string
          example: 3df296d2ec98136b8988420c05ee
        - $ref: '#/EntitySchema'

And my usage is on the following endpoint:

    post:
      description: Instantiate a new relationship schema
      operationId: instantiateRelationshipSchema
      requestBody:
        description: Relationship schema to create
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/InstantiateRelationshipSchema'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/InstantiateRelationshipSchema'

The issue appears only when using the multipart/form-data side and submitting strings for the vertex_a and vertex_b. I've identified the offending code:

val, err := d.DecodeValue()
if err != nil {
return err
}
if err := func(d *jx.Decoder) error {
{{- template "json/dec" $el }}
return nil
}(jx.DecodeStr(val)); err != nil {

jx.DecodeStr(val) - val is a raw un-quoted string value which the decoder can't parse e.g. if I submit a multipart/form-data request with abc123 as vertex_a then val at #L253 will be abc123... However the jx Decoder is expecting a JSON string token: "abc123". Hence the invalid json type error.

I've fixed my issue for now by removing the oneOf for the vertexes and only allowing the string version.

@adrianduke adrianduke changed the title gen: jx.DecodeStr passed unquoted string value and fails jx.DecodeStr passed unquoted string value and fails Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant