Skip to content

Can a per-enum documentation value be supported by this library? #1000

@ddcruver

Description

@ddcruver

It would be useful to allow the schema to define JavaDocs for each enumeration value.

Possible Implementation:

{
  "$id": "https://example.com/schemas/math/Digit.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "string",
  "javaType": "com.example.api.math.Digit",
  "description": "All the digits.",
  "enum": [
    "Zero",
    "One",
    "Two",
    "Three",
    "Four",
    "Five",
    "Six",
    "Seven",
    "Eight",
    "Nine"
  ],
  "enumDescriptions": {
    "Zero": "No digit.",
    "One": "The first digit",
    "Two": "The second digit",
    "Three": "The second digit",
    "Four": "...",
    "Five": "...",
    "Six": "...",
    "Seven": "...",
    "Eight": "...",
    "Nine": "..."
  },
  "required": [],
  "definitions": {},
  "additionalProperties": false
}
package com.example.api.math;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/** All The Digits */
public enum Digit {

    /** No digit. */
    ZERO("Zero"),

    /** Thre first digit. */
    ONE("One"),

    /** This second digit. */
    TWO("Two"),

    /** This third digit. */
    THREE("Three"),

    /** ... */
    FOUR("Four"),

    /** ... */
    FIVE("Five"),

    /** ... */
    SIX("Six"),

    /** ... */
    SEVEN("Seven"),

    /** ... */
    EIGHT("Eight"),

    /** ... */
    NINE("Nine");

    private final String value;
    private final static Map<String, Digit> CONSTANTS = new HashMap<String, Digit>();

    static {
        for (Digit c: values()) {
            CONSTANTS.put(c.value, c);
        }
    }

    private Digit(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return this.value;
    }

    @JsonValue
    public String value() {
        return this.value;
    }

    @JsonCreator
    public static Digit fromValue(String value) {
        Digit constant = CONSTANTS.get(value);
        if (constant == null) {
            throw new IllegalArgumentException(value);
        } else {
            return constant;
        }
    }

}

Since this is a generated class it would also possibly to store it and add a function to get it at runtime, but that may be feature creep.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions