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

Parameters read from JSON with SchemaXORContent could not be marshalled back - duplicate keys #142

Open
comdiv opened this issue Apr 27, 2024 · 0 comments

Comments

@comdiv
Copy link

comdiv commented Apr 27, 2024

Have scenario

  1. read _base.json as template of spec
  2. update it
  3. save as api.json

Got problem with parameters if they read from _base.json

  1. Minimal schema to reproduce
{
   "openapi": "3.1.0",
   "paths": {
         "/": {
         "get": {
            "parameters": [
               {
                  "name": "p",
                  "schema": {
                     "type": "string"
                  },
                  "in": "query"
               }
            ]
         }
      }
   }
}
  1. Read it
var spec *openapi3.Spec
if err = json.Unmarshal(errs.Must(os.ReadFile("./doc/api/_base.json")),&spec); err != nil { panic(err) }
  1. Try write it back
j, err = json.MarshalIndent(spec, "", "    ")
if err != nil { panic(err) }
fmt.Println(string(j))

will produce

{
   "openapi": "3.1.0",
   "paths": {
         "/": {
         "get": {
            "parameters": [
               {
                  "name": "p",
                  "schema": {
                     "type": "string"
                  },
                  "in": "query",
                  "schema": {
                     "type": "string"
                  },
                  "in": "query"
               }
            ]
         }
      }
   }
}
  1. The problem is - the combination of Parameter.SchemaXORContent,Parameter.Location and unionMarshal

SchemaXORContent is used during unmarshal as buffer, and then used back in marshalling but without any key control of
unique in unionMarhal

  1. Avoid -

For our case i have added

func ReadSpecForUpdate(data []byte) (*openapi3.Spec, error) {
	var spec *openapi3.Spec
	if err := json.Unmarshal(data,&spec); err != nil { return nil, err }
	for _, path := range spec.Paths.MapOfPathItemValues {
		for _, op := range path.MapOfOperationValues {
			for _, p := range op.Parameters {
				// todo - some information could be lost ???
				p.Parameter.SchemaXORContent = nil
                                p.Parameter.Location = nil
			}
		}
	}
	return spec, nil	
}
  1. Suggestion - rewrite unionMarshal - prepare data to avoid twice - pre merge data
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