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

Dirty-fix for known bug #307: Ignore keys containing '$'. #481

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

anatolPavl
Copy link
Contributor

Here is the URL of the known bug i am referring to: Bug-307

I encountered the same issue when attempting to create a lib for the OpenFeatureOperators CRDs.

This is only a quick and dirty "fix". It basically just ignores the field containing the $ sign. This allows at least the rest of the libsonnet to be generated.

Additionally a message is written to the Log, informing, that certain fields have been ommited.

@xvzf
Copy link
Member

xvzf commented Dec 5, 2024

As already stated in #482 - gonna check this in the next weeks to make sure it doesn't have a side-effect

@frimik
Copy link
Contributor

frimik commented Mar 29, 2025

Refers to #307.

I can't say I fully understand the use of these $foo properties in the openapi schema.. is it purely stylistic or no?

But to generate valid jsonnet, a couple of name = strings.TrimPrefix(name, "$") calls generates the following for the openfeature-libsonnet example:

  spec: {
    '#flagSpec':: d.obj(help='"FlagSpec is the structured representation of the feature flag specification"'),
    flagSpec: {
      '#withEvaluators':: d.fn(help='', args=[d.arg(name='evaluators', type=d.T.object)]),
      withEvaluators(evaluators): { spec+: { flagSpec+: { evaluators: evaluators } } },
      '#withEvaluatorsMixin':: d.fn(help='\n\n**Note:** This function appends passed data to existing values', args=[d.arg(name='evaluators', type=d.T.object)]),
      withEvaluatorsMixin(evaluators): { spec+: { flagSpec+: { evaluators+: evaluators } } },
      '#withFlags':: d.fn(help='', args=[d.arg(name='flags', type=d.T.object)]),
      withFlags(flags): { spec+: { flagSpec+: { flags: flags } } },
      '#withFlagsMixin':: d.fn(help='\n\n**Note:** This function appends passed data to existing values', args=[d.arg(name='flags', type=d.T.object)]),
      withFlagsMixin(flags): { spec+: { flagSpec+: { flags+: flags } } },
    },
  },

Here's what I hacked really quicky to try it:

diff --git a/pkg/model/modifiers.go b/pkg/model/modifiers.go
index 5833521..754188d 100644
--- a/pkg/model/modifiers.go
+++ b/pkg/model/modifiers.go
@@ -94,6 +94,8 @@ func newModifier(name string, p *swagger.Schema, ctx string, inArray bool,
 	defArray bool) (string, interface{}) {
 	name = CamelLower(name)
 
+	name = strings.TrimPrefix(name, "$")
+
 	switch p.Type {
 	case swagger.TypeArray:
 		// when defArray is true, create modifier directly
@@ -147,15 +149,15 @@ func fnArg(name string) string {
 		"then", "self", "super", "true":
 		return normalizedTitle(name)
 	default:
-		return name
+		return strings.TrimPrefix(name, "$")
 	}
 }
 
 // normalizedTitle normalizes a name and applied strings.Title()
 func normalizedTitle(name string) string {
-	if strings.HasPrefix(name, "-") {
-		name = strings.TrimPrefix(name, "-")
-	}
+	name = strings.TrimPrefix(name, "-")
+
+	name = strings.TrimPrefix(name, "$")
 
 	name = strings.Replace(name, ".", "_", -1)
 

@anatolPavl
Copy link
Contributor Author

I encountered the issue first when generating the jsonnet-libs for the CRDs of the Open-Feature-Operator. The problematic field in question was FeatureFlag.spec.flagSpec.$evaluators

I have tested how k8s reacts to "$evaluators" being referenced to as "evaluators":

# feature-flag.yaml
apiVersion: core.openfeature.dev/v1beta1
kind: FeatureFlag
metadata:
  name: my-feature-flag
  namespace: open-feature-test
spec:
  flagSpec:
    flags:
      myFeature:
        state: "ENABLED"
        variants:
          default: true
          off: false
        defaultVariant: default
    evaluators: {}
> kubectl apply -f feature-flag.yaml
> Error from server (BadRequest): error when creating "feature-flag.yaml": FeatureFlag in version "v1beta1" cannot be handled as a FeatureFlag: strict decoding error: unknown field "spec.flagSpec.evaluators"

I would suggest ignoring the "$foo" fields (for now, until it is properly fixed). This makes the libraries potentially incomplete, but at least the generated yaml will work 100% of the time. I mean if the CRD calls for a "$foo" field, it would not do anything with a "foo" field anyways.

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