Skip to content

Conversation

@karimassaly
Copy link

@karimassaly karimassaly commented Mar 20, 2025

Contexte to this upgrade

We currently do not have the possibility to decide weither a value should omit empty or not once the openAPI format is transcribed to Golang structures.

I have added a new value to the schema and reference structures called Extensions. This value will be able to handle all extra extensions we would like our components to hold. As of now only the "x-omitempty" is handled as it will search the json tag in a structure and place the value of this extension either to true or false.

But many other extensions could be added later on.

@karimassaly karimassaly changed the title feat(openapi/generator): add the x-omitempty extension to true on all… feat(openapi/generator): add the x-omitempty extension Mar 20, 2025
@tbourrelovh
Copy link

Hi,
Let me add more context.
We use fizz to generate our API specifications, and then rely on go-swagger to generate client code so that our customers can use it to call the API.

We observed that go-swagger adds the following struct tag on fields by default : json:"omitempty".
An issue on go-swagger states that to override this behavior, the x-omitempty openapi extension must be set.

We want to be able to have x-omitempty extension supported in order to have a generated client aligned with the api and avoid misleading our users.

… fields that contains an omitempty in json tag
@karimassaly karimassaly force-pushed the feature/add-omitempty-extension branch from 5883cf3 to b8071b6 Compare March 24, 2025 10:07
type Reference struct {
Ref string `json:"$ref" yaml:"$ref"`
Ref string `json:"$ref" yaml:"$ref"`
Extensions map[string]interface{} `json:"-" yaml:"-"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the rationale for this field addition.
Extensions should only be present on the Schema struct. It can then be used inline or be references through the spec components.

Extensions map[string]interface{} `json:"-" yaml:"-"`
}

func (s *Schema) MarshalJSON() ([]byte, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I get it right, you're trying to simulate the new omitnil JSON field tag, to avoid marshaling the Extensions field it it's empty ? What the purpose.
omitnil could be used (from go1.24), and for previous versions, it doesn't really matter if we marshal extensions: [].

@wI2L
Copy link
Owner

wI2L commented Apr 2, 2025

@karimassaly

With your current implementation, the new extension is always enabled.
We currently use OperationOption in order to configure each operation individually.

For this usecase, I envision the addition of a new GeneratorOption type that would allow to configure the behavior of the generator when it's initialized with NewGenerator. The pattern would allow to pass a variadic list of options to New and NewFromEngine, which would be backward compatible.

The option func could be named WithExtension, to be generic, and it would itself accept a variadic list of extensions names (from constants). Store the content in a map (for O(1) lookup) or a new generatorExtensions struct with boolean fields if that's more convenient, and use the condition where that's necessary to decide if the extensions should be enabled (in the addExtensions for example).

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

Successfully merging this pull request may close these issues.

3 participants