asyncapi / modelina

A library for generating typed models based on inputs such as AsyncAPI, OpenAPI, and JSON Schema documents with high customization
https://modelina.org
Apache License 2.0
302 stars 176 forks source link

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

Open jonaslagoni opened 1 year ago

jonaslagoni commented 1 year ago

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.
github-actions[bot] commented 1 year ago

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:

AnimeshKumar923 commented 9 months ago

@jonaslagoni still valid?

jonaslagoni commented 9 months ago

@AnimeshKumar923 yes, this is still valid.

skushagra9 commented 8 months ago

I would like to work on this

akkshitgupta commented 8 months ago

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

jonaslagoni commented 8 months ago

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."
                            }
                        }
                    }
                }
            }
        }
    }
}
github-actions[bot] commented 4 months ago

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: