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

bug in parsing array param in body #565

Closed
sdghchj opened this issue Nov 21, 2019 · 6 comments
Closed

bug in parsing array param in body #565

sdghchj opened this issue Nov 21, 2019 · 6 comments

Comments

@sdghchj
Copy link
Member

sdghchj commented Nov 21, 2019

I have a comment as:

// @Param body body []integer true "array integer"

However it was parsed as follow in my docs.go:

{
    "description": "array integer",
    "name": "body",
    "in": "body",
    "required": true,
    "schema": {
                "type": "integer",   //here I think it shoud be "array"
                "items": {
                     "type": ["integer"]
                 }
    }
}

Schema type was parsed as integer, so it was displayed as follows :
image
image

But I want it to be displayed as array. After I edited my docs.go as :

{
    "description": "array integer",
    "name": "body",
    "in": "body",
    "required": true,
    "schema": {
                "type": "array", 
                "items": {
                     "type": "integer"
                 }
    }
}

then displayed as what I expected:
image
image

I found code here param.Schema.Type is default assigned by refType,which I think shoud be by objectType.

param := createParameter(paramType, description, name, refType, required)

and from here, param.Schema.Type have still not been reassigned . I think it's a bug.

@ubogdan
Copy link
Contributor

ubogdan commented Nov 21, 2019

Since integer is not a valid go primitive we can guess integer is a user-defined type. Try

// @Param body body []int true "array integer"

@sdghchj
Copy link
Member Author

sdghchj commented Nov 21, 2019

@ubogdan
I have tried []int at first. it does'nt work.

Read code here and swagger primitive please.

int is considered to be user-defined type, not integer.

Moreover, the key issue is on how to parse [ ],not int or integer.

@ubogdan
Copy link
Contributor

ubogdan commented Nov 21, 2019

OK. Thanks.

@sdghchj
Copy link
Member Author

sdghchj commented Nov 22, 2019

I modified code

case "body":
		switch objectType {
		case "primitive":
			param.Schema.Type = spec.StringOrArray{refType}
		case "array":
			param.Schema.Type = spec.StringOrArray{objectType} // add this line
			param.Schema.Items = &spec.SchemaOrArray{
				Schema: &spec.Schema{
					SchemaProps: spec.SchemaProps{},
				},
			}
			// Array of Primitive or Object
			if IsPrimitiveType(refType) {
                                //add following two lines
				param.SimpleSchema.Type = objectType
				param.SimpleSchema.Items = &spec.Items{SimpleSchema: spec.SimpleSchema{Type: refType}}
				param.Schema.Items.Schema.Type = spec.StringOrArray{refType}
			} else {
				if err := operation.registerSchemaType(refType, astFile); err != nil {
					return err
				}
				param.Schema.Items.Schema.Ref = spec.Ref{Ref: jsonreference.MustCreateRef("#/definitions/" + refType)}
			}

then displayed:

image

This is perfect for me.

@Canecat
Copy link

Canecat commented Dec 9, 2019

@sdghchj Could you post the pull request for this issue? :)

@easonlin404
Copy link
Member

fix via #579. please reopen it if there are still any issues.

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

4 participants