modelina icon indicating copy to clipboard operation
modelina copied to clipboard

JSON Schema enum pattern to define enum key and value with oneOf

Open jonaslagoni opened this issue 2 years ago • 7 comments

Reason/Context

A new pattern emerged for JSON Schema that enable you to define enums with key and value.

Severity:
  type: integer
  oneOf:
    - title: HIGH
      const: 2
      description: An urgent problem
    - title: MEDIUM
      const: 1
    - title: LOW
      const: 0
      description: Can wait forever

Which expect to be rendered as the following:

public enum Source {
  HIGH(2), MEDIUM(1), LOW(0);
}

But currently is split out into 3 enums:

public enum Low {
  NUMBER_0((int)0);

  private int value;

  Low(int value) {
    this.value = value;
  }
  ...
}

To solve this I expect the following change is needed:

  1. We need to adapt the CommonModel to allow enums to be defined with key and value instead of just value.
  2. We need to adapt the CommonModel to MetaModel conversion to respect the new key and value
  3. We need to adapt the interpreter to look for this pattern in oneOf and anyOf, maybe a post process of the keyword interpretation we can merge all enums together.

jonaslagoni avatar Feb 02 '23 18:02 jonaslagoni

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] avatar Jun 03 '23 00:06 github-actions[bot]

@jonaslagoni still valid?

AnimeshKumar923 avatar Jan 07 '24 06:01 AnimeshKumar923

@AnimeshKumar923 yes, this is still valid.

jonaslagoni avatar Jan 07 '24 20:01 jonaslagoni

I would like to work on this

skushagra9 avatar Jan 21 '24 09:01 skushagra9

Hey @jonaslagoni can you please share how one can reproduce or test this particular thing in local development.

akkshitgupta avatar Feb 06 '24 10:02 akkshitgupta

You can use the following input:

{
    "asyncapi": "2.5.0",
    "info": {
        "title": "Streetlights API",
        "version": "1.0.0",
        "description": "The Smartylighting Streetlights API allows you\nto remotely manage the city lights.\n",
        "license": {
            "name": "Apache 2.0",
            "url": "https://www.apache.org/licenses/LICENSE-2.0"
        }
    },
    "servers": {
        "mosquitto": {
            "url": "mqtt://test.mosquitto.org",
            "protocol": "mqtt"
        }
    },
    "channels": {
        "light/measured": {
            "publish": {
                "summary": "Inform about environmental lighting conditions for a particular streetlight.",
                "operationId": "onLightMeasured",
                "message": {
                    "name": "LightMeasured",
                    "payload": {
                        "type": "object",
                        "$id": "LightMeasured",
                        "properties": {
                            "id": {
                                "type": "integer",
                                "minimum": 0,
                                "description": "Id of the streetlight."
                            },
                            "lumens": {
                                "type": "integer",
                                "oneOf": [
                                    {
                                        "title": "HIGH",
                                        "const": 2,
                                        "description": "An urgent problem"
                                    },
                                    {
                                        "title": "MEDIUM",
                                        "const": 1
                                    },
                                    {
                                        "title": "LOW",
                                        "const": 0,
                                        "description": "Can wait forever"
                                    }
                                ]
                            },
                            "sentAt": {
                                "type": "string",
                                "format": "date-time",
                                "description": "Date and time when the message was sent."
                            }
                        }
                    }
                }
            }
        }
    }
}

jonaslagoni avatar Feb 06 '24 14:02 jonaslagoni