oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Support for YAML struct tags instead of JSON.

Open liamkinne opened this issue 3 years ago • 5 comments

For my use case I'd like the struct tags to be for YAML instead of JSON. I've tried 'applicaion/yaml' in the response type of the API spec had a look at the code and it doesn't seem this project supports that out of the box.

What I get

type Device struct {
	Messages *[]Message `json:"messages,omitempty"`
}

What I want

type Device struct {
	Messages *[]Message `yaml:"messages,omitempty"`
}

If this isn't already a feature, or on the road map I am happy to fork and give implementing this a crack. Just wanted to get a second pair of eyes to confirm.

liamkinne avatar Nov 11 '22 10:11 liamkinne

For anyone that comes across this and wants a quick fix. sed to the rescue.

sed -i '' "s/json:/yaml:/" api.gen.go

liamkinne avatar Nov 11 '22 11:11 liamkinne

To confirm, is this where you're expecting to produce an application/yaml formatted API response?

jamietanna avatar Nov 12 '22 20:11 jamietanna

@jamietanna yes, produce (with server generation) or consume (with client generation) yaml formatted data.

liamkinne avatar Nov 18 '22 13:11 liamkinne

I would like to see this as well. I don't mind attempting a PR for it, assuming it will get moved through. Based on the comments on GenFieldsFromProperties(), the generation happens in that func, and specifically these lines.

It shouldn't be too hard to add a similar section for yaml, with p.yamlFieldName (which might or might not be identical to p.jsonFieldName. I would have to dig a bit to see where it is set.

Interested?

deitch avatar May 08 '24 10:05 deitch

It shouldn't be too hard to add a similar section for yaml, with p.yamlFieldName (which might or might not be identical to p.jsonFieldName

Actually, I overthought this. By definition, jsonFieldName must be the same as the field name for yaml, as it is the field from the original spec that generated this. So this should be easier than I thought, just adding a few lines there.

@jamietanna want a short PR for it?

deitch avatar May 09 '24 10:05 deitch