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

Enum constants not prefixed with type name

Open adriansuarez opened this issue 9 months ago • 3 comments

After expanding the set of tags (-include-tags) that we generate code for, the type prefix that used to appear on enum constants vanishes, breaking existing code.

The unexpected changes are the following:

diff --git a/openapi/types.go b/openapi/types.go
index 4dbefa8..ae56387 100644
--- a/openapi/types.go
+++ b/openapi/types.go
@@ -55,14 +55,14 @@ const (

 // Defines values for ProjectStatusModelState.
 const (
-       ProjectStatusModelStateAvailable ProjectStatusModelState = "Available"
-       ProjectStatusModelStateCreating  ProjectStatusModelState = "Creating"
-       ProjectStatusModelStateDeleting  ProjectStatusModelState = "Deleting"
-       ProjectStatusModelStateExpired   ProjectStatusModelState = "Expired"
-       ProjectStatusModelStateFailed    ProjectStatusModelState = "Failed"
-       ProjectStatusModelStateModifying ProjectStatusModelState = "Modifying"
-       ProjectStatusModelStateStopped   ProjectStatusModelState = "Stopped"
-       ProjectStatusModelStateStopping  ProjectStatusModelState = "Stopping"
+       Available ProjectStatusModelState = "Available"
+       Creating  ProjectStatusModelState = "Creating"
+       Deleting  ProjectStatusModelState = "Deleting"
+       Expired   ProjectStatusModelState = "Expired"
+       Failed    ProjectStatusModelState = "Failed"
+       Modifying ProjectStatusModelState = "Modifying"
+       Stopped   ProjectStatusModelState = "Stopped"
+       Stopping  ProjectStatusModelState = "Stopping"
 )

 // BackupCreateModel defines model for BackupCreateModel.

This schema was not changed at all. The only change is to -include-tags.

I pushed the following branch in our repo that exposes this issue: https://github.com/nuodb/terraform-provider-nuodbaas/tree/asuarez/enum-constants-issue

The issue can be reproduced by checking out that branch and running make generate.

adriansuarez avatar May 16 '24 18:05 adriansuarez

Agree, we have the same issue

0xUnkn0wN avatar Jul 09 '24 08:07 0xUnkn0wN

I have a problem that some of the constants are generated with prefix, some of them not.

// Defines values for CarType.
const (
	CarTypeCOMPARTMENT  CarType = "COMPARTMENT"
	CarTypeLUX          CarType = "LUX"
	CarTypeSOFT         CarType = "SOFT"
)

// Defines values for CarrierSpecType.
const (
	Marketing CarrierSpecType = "marketing"
	Plating   CarrierSpecType = "plating"
)

That's weird because after collisions met and regeneration Prefix will be added. And after that I will have to change source code.

[email protected]

UPD:

option: always-prefix-enum-values https://github.com/oapi-codegen/oapi-codegen/blob/main/configuration-schema.json#L87

SOLVED my issue

ilya-direct avatar Jul 29 '24 11:07 ilya-direct

I'm fine with closing this since @ilya-direct has provided a workaround and it seems that this is expected behavior, but I think the default behavior should be flipped.

It would make more sense for always-prefix-enum-values=true to be the default, and if someone really wants short enum constants at the cost of an unstable API, then they can explicitly specify always-prefix-enum-values=false.

adriansuarez avatar Oct 04 '24 21:10 adriansuarez