@@ -41,6 +41,7 @@ The configuration file includes the following:
41
41
- [ ` nonStandardFormat ` ] ( #nonstandardformat )
42
42
- [ ` customClasses ` ] ( #customclasses )
43
43
- [ ` classNames ` ] ( #classnames )
44
+ - [ ` annotations ` ] ( #annotations )
44
45
45
46
46
47
## ` title `
@@ -357,6 +358,9 @@ definition.
357
358
This use of URI to specify custom class selection has the distinct advantage that it may be used in conjunction with
358
359
schema files that are not open to modification, for example schema files that are read directly from public websites.
359
360
361
+ ** NOTE:** If a default value is given for a property that maps to a custom class, the code generator will output a
362
+ constructor for the custom class, with the default value as a single parameter.
363
+ If no such constructor exists, default values should be avoided.
360
364
361
365
## ` classNames `
362
366
@@ -374,3 +378,126 @@ This will cause the schema definition with the specified `$id` to be generated a
374
378
Note that in this case, the class name is ** not** a fully-qualified class name &ndash ; the package name used will be the
375
379
one specified with the [ ` packageName ` ] ( #packagename ) configuration option (possibly extended by the directory structure
376
380
&ndash ; see the [ ` derivePackageFromStructure ` ] ( #derivepackagefromstructure ) option).
381
+
382
+
383
+ ## ` annotations `
384
+
385
+ The code generator can be configured to add annotations to the generated classes and fields (properties).
386
+ The annotation may be a simple marker annotation with no parameters, or may have parameters formed from a
387
+ [ Mustache] ( https://github.com/pwall567/kotlin-mustache ) template.
388
+
389
+ For example, to add a ` @Generated ` annotation to each class:
390
+ ``` json
391
+ {
392
+ "annotations" : {
393
+ "classes" : {
394
+ "javax.annotation.Generated" : " \" {{&generator}}\" , date=\" {{&dateTime}}\" "
395
+ }
396
+ }
397
+ }
398
+ ```
399
+
400
+ This will result in an annotation similar to the following being added to each class:
401
+ ``` kotlin
402
+ import javax.annotation.Generated
403
+
404
+ @Generated(" net.pwall.json.schema.codegen.CodeGenerator" , date= " 2022-07-18T10:36:18.626+10:00" )
405
+ ```
406
+
407
+ The example illustrates several important points:
408
+ 1 . Class annotations are defined in a section headed ` classes ` (field annotations are in a section headed ` fields ` ).
409
+ 2 . The annotation name must be supplied as a fully-qualified class name.
410
+ The fully-qualified name will be used on an ` import ` , and just the class name will be used on the annotation itself.
411
+ 3 . The parameters for the annotation must be supplied together as a single string, and any special characters like
412
+ quotes must be escaped with backslashes as is normal for JSON (when using YAML, single-quoted strings allow any
413
+ special characters other than single quote itself).
414
+ No syntax checking is performed on the parameters, and any errors may result in code that will not compile.
415
+ The template must ** not** include the parentheses surrounding the parameters;
416
+ these will be added by the generator when required.
417
+ 4 . The context object used during Mustache template processing contains several variables that may be useful.
418
+ Some of these are listed [ below] ( #template-context-object ) .
419
+
420
+ A second example shows a field annotation with no parameters:
421
+ ``` json
422
+ {
423
+ "annotations" : {
424
+ "fields" : {
425
+ "com.example.anno.Demo" : null
426
+ }
427
+ }
428
+ }
429
+ ```
430
+
431
+ This will result in the simple annotation ` @Demo ` being added to each field with no parameters (and no parentheses),
432
+ with an ` import com.example.anno.Demo ` added to the start of the file.
433
+
434
+ This example shows:
435
+ 1 . Field annotations are defined in a section headed ` fields ` .
436
+ 2 . The absence of parameters is indicated by a ` null ` template string.
437
+
438
+ ### Template Context Object
439
+
440
+ The Mustache template expansion can take values from a "context object".
441
+ For class annotations, the context object contains the following:
442
+
443
+ | Name | Type | Description |
444
+ | ---------------| --------------| --------------------------------------------|
445
+ | ` className ` | ` String ` | the class name |
446
+ | ` packageName ` | ` String? ` | the package name (or ` null ` ) |
447
+ | ` source ` | ` String ` | the schema source file name (if available) |
448
+ | ` schema ` | ` JSONSchema ` | the schema as a ` JSONSchema ` |
449
+
450
+ For field annotations, the context object contains:
451
+
452
+ | Name | Type | Description |
453
+ | ---------------| -----------| ----------------------------------------------------------------------------|
454
+ | ` name ` | ` String ` | the field name |
455
+ | ` kotlinName ` | ` String ` | the name, usable as a Kotlin variable name (see below) |
456
+ | ` javaName ` | ` String ` | the name, usable as a Java variable name (see below) |
457
+ | ` isObject ` | ` Boolean ` | ` true ` if the field type is ` object ` |
458
+ | ` isArray ` | ` Boolean ` | ` true ` if the field type is ` array ` |
459
+ | ` isString ` | ` Boolean ` | ` true ` if the field type is ` string ` |
460
+ | ` isBoolean ` | ` Boolean ` | ` true ` if the field type is ` boolean ` |
461
+ | ` isDecimal ` | ` Boolean ` | ` true ` if the field type is ` decimal ` |
462
+ | ` isIntOrLong ` | ` Boolean ` | ` true ` if the field type is ` integer ` |
463
+ | ` isInt ` | ` Boolean ` | ` true ` if the field type is ` integer ` and will fit in an ` Int ` (see below) |
464
+ | ` isLong ` | ` Boolean ` | ` true ` if the field type is ` integer ` and will not fit in an ` Int ` |
465
+ | ` isRequired ` | ` Boolean ` | ` true ` if the field appears in a ` required ` constraint |
466
+
467
+ The ` kotlinName ` will be the same as the ` name ` , except when it contains characters unacceptable in a Kotlin variable
468
+ name (such as spaces) or when it is a reserved word (like ` val ` ).
469
+ In these cases, the ` kotlinName ` is the original name enclosed in backticks.
470
+
471
+ Java does not have the backtick mechanism, so ` javaName ` is the original name with unacceptable characters replaced by
472
+ underscore, and names clashing with reserved words being suffixed with underscore.
473
+
474
+ A field that is declared to be an ` integer ` will be generated as a ` Long ` , unless there are ` minimum ` and ` maximum `
475
+ constraints limiting it to 32 bits (signed).
476
+
477
+ The ` Boolean ` values may be used in a Mustache conditional "section", as follows:
478
+ ``` json
479
+ {
480
+ "annotations" : {
481
+ "fields" : {
482
+ "com.example.anno.Demo" : " \" Field {{name}} is {{^isString}}not {{/isString}}a String\" "
483
+ }
484
+ }
485
+ }
486
+ ```
487
+
488
+ ### Parent Context Object
489
+
490
+ Mustache name resolution uses a chain of context objects; if a name is not found in the current context object,
491
+ resolution switches to the parent context object, then to the parent's parent, and so on.
492
+ For both class annotations and field annotations, the parent context object provides information on the current code
493
+ generation run:
494
+
495
+ | Name | Type | Description |
496
+ | -------------| ------------------| -----------------------------------------------------------|
497
+ | ` dateTime ` | ` OffsetDateTime ` | the date/time of the current generation run |
498
+ | ` date ` | ` LocalDate ` | the date portion of the above date/time |
499
+ | ` time ` | ` LocalTime ` | the time portion of the above date/time |
500
+ | ` generator ` | ` String ` | the code generator name, suitable for use in ` @Generated ` |
501
+ | ` uuid ` | ` UUID ` | a random UUID, for tagging a specific build |
502
+
503
+ The use of this information is shown in the first example in the [ ` annotations ` ] ( #annotations ) section.
0 commit comments