Skip to content

remove confusing type Enum or String (a set of values could not be closed and open) #215

Open

Description

@davidB

With version 0.4, ticket's schema define type of some field (priority, resolution, ticketType) like:

"anyOf": [
  {
    "type": "string",
    "enum": [
      "low",
      "medium",
      "high"
    ]
  },
  {
    "type": "string"
  }
]

But this definition is "conflicting" as the second is a superset of the first. (if oneOf is used instead of anyOf a jsonschema linter will warm about this issue). I mean for any language implementation string will be used (or an automatic generator/serializer+deserializer will be confused).

What is the purpose/value of the enum if any value can be used? (eg if a system is uppercase like "LOW")

Suggestion: replace the anyOf by single"type": "string"

@sbrennan4 WDYT?

Activity

  1. xibz commented on May 20, 2024

    @xibz
    Contributor

    Oh! I completely mistook what you were saying! Gotcha! hahah, now I see the confusion. Okay, yea, this is definitely not modeled correctly. Okay, I will have this fixed with

    "enum": [
          "low",
          "medium",
          "high"
        ]
  2. davidB commented on May 20, 2024

    @davidB
    ContributorAuthor

    if a, b, c should not be represented in code, then enum is not the right json-schema medium. It's why I suggest examples.
    Also note that using examples will also help to be in sync in the md files (and maybe to generate part of them) and tool like schemathesis,...

  3. xibz commented on May 20, 2024

    @xibz
    Contributor

    Okay, I think I understand what was trying to be modeled. So basically we want any string, but want to provide some defaults, like low, medium, high in the SDKs. I think this is primarily used to generate some defaults in the SDK.

    Just providing string is fine, and having it documented that the string is low, medium, or high, but that isn't as useful as providing that in an SDK.

  4. removed
    bug
    on May 20, 2024
  5. afrittoli commented on May 20, 2024

    @afrittoli
    Member

    oneOf is better IMHO in term of types and how to deserialize data. oneOf is like XOR, a value can be A or B but not both. I guess that a json validator will failed with the current definition because the enum and the string are not mutually exclusive (eg with a "low"). oneOf use discriminator or "duck typing" to check that there is no overlap between possible type.

    IMO, oneOf highlights the issue. Using the enum to show some possible value is not the right approach.

    To be clear what will work for me (and for tool and reader) is something like:

        "priority": {
          "description": "An indicator of the importance of the ticket",
          "examples": [
              "low",
              "medium",
              "high"
          ],    
          "type": "string"
        }

    Interesting - is examples here a custom field? Does jsonschema allow for that?
    I would like the SDK to include several constants which correspond to the values in examples, so that SDK users can benefit from those or add their own value - would it be possible to achieve that with a custom schema field?

  6. davidB commented on May 20, 2024

    @davidB
    ContributorAuthor

    examples is part of json-schema draft 6, see https://json-schema.org/understanding-json-schema/reference/annotations

    and used by some OpenAPI and contract-testing tool like schemathesis to validate API or generate samples or validate API

  7. xibz commented on May 20, 2024

    @xibz
    Contributor

    @davidB Examples are nice, but I think it's really important to have the values modeled in the SDKs. I believe examples, will not be used in code generation? So it may not be as useful

  8. davidB commented on May 20, 2024

    @davidB
    ContributorAuthor

    @xibz Can you provide an example (in a programming language) about how you think those values could be modeled in the SDKs?

  9. xibz commented on May 20, 2024

    @xibz
    Contributor

    @davidB

    type Priority string
    
    var Low Priority = "low"
    var Medium Priority = "medium"
    var High Priority = "high"

    Something like this would be sufficient

    Then if a user wanted a custom value they could do something like

    fmt.Println(cdevents.Priority("my-custom-value"))
    // or use a predefined value we've specified to help consistency
    fmt.Println(cdevents.Low)
  10. added this to the v0.5 milestone on May 20, 2024
  11. afrittoli commented on May 20, 2024

    @afrittoli
    Member

    @xibz We discussed this during the working group today and came to this proposal, let us know what you think:

    • for v0.4.0 SDKs, we will hardcode string as the type for fields that use the anyOf
    • in v0.5 we will change the jsonschema to what @davidB proposed:
    "priority": {
          "description": "An indicator of the importance of the ticket",
          "examples": [
              "low",
              "medium",
              "high"
          ],    
          "type": "string"
        }
    • in v0.5 SDKs can expose values from the examples field using some custom code in the generator, e.g.
    type Priority string
    
    var Low Priority = "low"
    var Medium Priority = "medium"
    var High Priority = "high"

    FYI @e-backmark-ericsson @rjalander

  12. xibz commented on May 20, 2024

    @xibz
    Contributor

    @afrittoli I think that makes sense except for generating based on examples. Mostly that examples does not seem like the appropriate place to generate defaults from. However, if we want to change the semantics of examples then Im fine with it.

    This isn't used for validation, but may help with explaining the effect and purpose of the schema to a reader

    While we could generate from there, other enums from examples may cause issues if we do not plan to have them as generated. So it feels hacky.

    Im personally fine with the current definition, as it is valid. However, if there is a more correct way to write it, I'd be for it. I don't know if examples is the correct way though. With that said, though, Im not too overly worried about it and I think if it unblocks @davidB, we can move forward with it in v0.5.

  13. afrittoli commented on Jul 22, 2024

    @afrittoli
    Member

    Different modelling languages might allows us to express this better, so that's something we should look into.

  14. moved this from Todo to Backlog in CDEvents Releaseson Oct 21, 2024
  15. moved this from Backlog to Todo in CDEvents Releaseson Nov 11, 2024
  16. self-assigned this
    on Nov 11, 2024
  17. xibz commented on Dec 2, 2024

    @xibz
    Contributor

    cdevents/implementation-wg#5

    I believe a new modeling language(s) may help with this, but we may still need some consistency across definitions, specifically enums.

    {
      "enum": "value"
    }
    class Foo {
       public String enum;
    }
    
    class Foo {
      public StringEnum enum;
    }
    
    StringEnum | FooEnum | ValueEnum
    
    "string-enum/<enum-value>"
    
  18. afrittoli commented on Feb 10, 2025

    @afrittoli
    Member

    A solution to this depends on the work being done on the modelling language, as long as the language supports modelling it.
    @xibz example: https://stackoverflow.com/questions/3978654/best-way-to-create-enum-of-strings

    We've not used enum properly so far, so what we need is:

    • from a validation point of view, we only have strings
    • we want to have constants in the SDK somewhere that can be used by downstream consumers as recognised special values
  19. changed the title [-]remove confusing type `Enum or String`[/-] [+]remove confusing type `Enum or String` (a set of values could not be closed and open)[/+] on Feb 12, 2025
  20. removed this from the v0.5 milestone on Jul 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions