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

how to add a request body parameter to an operation #1025

Open
jmls opened this issue Oct 24, 2024 · 0 comments
Open

how to add a request body parameter to an operation #1025

jmls opened this issue Oct 24, 2024 · 0 comments

Comments

@jmls
Copy link

jmls commented Oct 24, 2024

This isn't a bug - I'm trying to figure out how to add a request body parameter to an operation

I have an openapi spec file, but one of the operations does not have a body parameter for a POST and I was trying to be smart and add it in processing the json with kin

A cunning and daring plan. With 2 small flaws

a) I don't know Go
b) I don't know Go

I have tried to work my way through tests, sample code and even lowered myself to use Gemini and Chat Gtp (bloody useless) so I am throwing myself at the mercy of this community

the payload is a json object with any number of keys and a value

{"foo":"bar","baz":"abc"}

etc

I have got so far

func addBodyParameter(operation *openapi3.Operation) {
	payloadSchema := &openapi3.SchemaRef{Value: &openapi3.Schema{Type: &openapi3.Types{"object"},
		AdditionalProperties: &openapi3.Schema{Type: &openapi3.Types{"string"}},
	}}

	mediaType := &openapi3.MediaType{
		Schema: payloadSchema,
	}


	requestBody := &openapi3.RequestBody{
		Required: true,
		Content: openapi3.Content{
			"application/json": mediaType,
		},
	}

	operation.RequestBody = &openapi3.RequestBodyRef{
		Value: requestBody,
	}

	log.Println("xx", payloadSchema)
}

	for path, pathItem := range doc.Paths.Map() { // doc.Paths is of type *openapi3.Paths

		for _, operation := range []*openapi3.Operation{
			pathItem.Get, pathItem.Post, pathItem.Put, pathItem.Delete, pathItem.Patch,
		} {
				if operation.OperationID == "cubbyhole-write" {
					addBodyParameter(operation)

				}

I think that I just can;t find the right syntax for payloadSchema

Would appreciate any help

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