@@ -28,13 +28,34 @@ public class JsonLdEnumSerializer extends JsonSerializer<Enum<?>> {
2828
2929 @ Override
3030 public void serialize (Enum value , JsonGenerator gen , SerializerProvider provider ) throws IOException {
31+ //Generated Enum classes of the admin shell have @IRI annotations, which need to be used to provide proper RDF
3132 if (value .getClass ().isEnum () && value .getClass ().getName ().startsWith ("io.adminshell.aas." ))
3233 {
34+ //Try to get annotation value to get the IRI used in the ontology
3335 if (value .getClass ().getAnnotation (IRI .class ) != null && value .getClass ().getAnnotation (IRI .class ).value ().length > 0 )
3436 {
3537 gen .writeStartObject ();
3638 gen .writeStringField ("@type" , value .getClass ().getAnnotation (IRI .class ).value ()[0 ]);
37- gen .writeStringField ("@id" , translate (value .getClass (), value .name ()));
39+
40+ //Try to extract exact IRI of the enum value, if present
41+ try {
42+ var annotation = value .getClass ().getField (value .name ()).getAnnotation (IRI .class );
43+ if (annotation != null && annotation .value ().length > 0 )
44+ {
45+ gen .writeStringField ("@id" , annotation .value ()[0 ]);
46+ }
47+ else
48+ {
49+ //Didn't find an @IRI annotation - fall back to using class annotation + field name
50+ gen .writeStringField ("@id" , translate (value .getClass (), value .name ()));
51+ }
52+ }
53+ //Should be impossible
54+ catch (NoSuchFieldException e )
55+ {
56+ //Didn't find an @IRI annotation - fall back to using class annotation + field name
57+ gen .writeStringField ("@id" , translate (value .getClass (), value .name ()));
58+ }
3859 gen .writeEndObject ();
3960 }
4061 else
0 commit comments