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

Wrong type detect for Example of a String field in YAML #2450

Open
xlab opened this issue Jan 13, 2020 · 11 comments
Open

Wrong type detect for Example of a String field in YAML #2450

xlab opened this issue Jan 13, 2020 · 11 comments
Assignees

Comments

@xlab
Copy link

xlab commented Jan 13, 2020

Hello! I'm trying to generate API spec for a design like this:

package design

import (
	. "goa.design/goa/v3/dsl"
)

var _ = API("APITest", func() {

})

var _ = Service("ServiceTest", func() {
	Method("Main", func() {
		Payload(func() {
			Attribute("field1", String, func() {
				Example("0xffff")
			})
			Attribute("field2", String, func() {
				Example(`"0xffff"`)
			})
			Attribute("field3", String, func() {
				Example("0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498")
			})
			Attribute("field4", String, func() {
				Example(`"0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498"`)
			})
		})

		HTTP(func() {
			GET("/")
		})
	})
})

The problem is that the generated Swagger spec looks like this:

  ServiceTestMainRequestBody:
    title: ServiceTestMainRequestBody
    type: object
    properties:
      field1:
        type: string
        example: "0xffff"
      field2:
        type: string
        example: '"0xffff"'
      field3:
        type: string
        example: 0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498
      field4:
        type: string
        example: '"0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498"'
    example:
      field1: "0xffff"
      field2: '"0xffff"'
      field3: 0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498
      field4: '"0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498"'

As you can see, for some reason field3 is treated as a number! Which will affect the generated documentation later. And for some reason field1 is treated correctly.

So that's clearly a bug, I'm not sure why Goa tries to dynamically detect type if it is declared as String already in the spec. @raphael I need your help at this point :)

@xlab xlab changed the title Wrong type detect for String in examples Wrong type detect for Example of a String field Jan 13, 2020
@xlab
Copy link
Author

xlab commented Jan 13, 2020

Clarification: the bug is somewhere near YAML generation, because JSON looks ok:

   "definitions": {
        "ServiceTestMainRequestBody": {
            "title": "ServiceTestMainRequestBody",
            "type": "object",
            "properties": {
                "field1": {
                    "type": "string",
                    "example": "0xffff"
                },
                "field2": {
                    "type": "string",
                    "example": "\"0xffff\""
                },
                "field3": {
                    "type": "string",
                    "example": "0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498"
                },
                "field4": {
                    "type": "string",
                    "example": "\"0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498\""
                }
            },
            "example": {
                "field1": "0xffff",
                "field2": "\"0xffff\"",
                "field3": "0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498",
                "field4": "\"0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498\""
            }
        }
    }

@raphael
Copy link
Member

raphael commented Jan 14, 2020

Thank you for the report! Goa does not do any type inference. This looks like some weirdness in the YAML package that Goa uses (or Goa not using it correctly).

@raphael raphael added the bug label Jan 14, 2020
@xlab xlab closed this as completed Jan 14, 2020
@xlab xlab reopened this Jan 14, 2020
@xlab xlab changed the title Wrong type detect for Example of a String field Wrong type detect for Example of a String field in YAML Jan 14, 2020
@saniales
Copy link
Member

@raphael actually since YAML accepts strings only, we can actually put the "" under every field and solving this issue, what do you think?

@raphael
Copy link
Member

raphael commented May 19, 2020

@saniales if that works then sounds good! I believe the issue is that sometimes the YAML package will add these quotes and sometimes it won't which means that if we always add them it might double escape them?

@xlab
Copy link
Author

xlab commented May 20, 2020

@saniales

actually since YAML accepts strings only

Well not only strings - YAML natively encodes scalars (such as strings, integers, and floats)

@saniales
Copy link
Member

@raphael @xlab what we actually care is not to be pure about yaml types, instead that they are shown correctly in openapi spec, being string the type we can stringify everything and it should work

@xlab
Copy link
Author

xlab commented May 20, 2020

@saniales I agree, that might be the approach

@raphael
Copy link
Member

raphael commented May 25, 2020

@saniales give it a shot, I suspect that the result is going to be that they are all strings but that "sometimes" the string will be quoted which may or may not be an issue for OpenAPI spec examples.

@saniales
Copy link
Member

will give it a shot in the coming days

@saniales saniales self-assigned this May 25, 2020
@saniales
Copy link
Member

saniales commented Dec 28, 2021

I confirm that this can be solved by using single quotes only with string values

And I also confirm that JSON generated openapi is not affected by this, only YAML

@raphael
Copy link
Member

raphael commented Jan 5, 2022

Would you care to make a PR that adds the quote?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants