@cparra/apexdocs 2.9.0-alpha.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +268 -9
  2. package/docs/Main/SampleClass.md +189 -188
  3. package/docs/Misc-Group/ChildClass.md +5 -2
  4. package/docs/Misc-Group/ParentClass.md +3 -3
  5. package/docs/Misc-Group/Reference1.md +18 -0
  6. package/docs/Misc-Group/Reference2.md +7 -0
  7. package/docs/Misc-Group/Reference3.md +7 -0
  8. package/docs/Misc-Group/Reference4.md +7 -0
  9. package/docs/Misc-Group/Reference5.md +7 -0
  10. package/docs/Misc-Group/Reference6.md +9 -0
  11. package/docs/Misc-Group/SampleRestResource.md +109 -0
  12. package/docs/Misc-Group/SampleRestResourceWithInnerClass.md +37 -0
  13. package/docs/README.md +30 -0
  14. package/docs/openapi.json +127 -4
  15. package/examples/force-app/main/default/classes/SampleClass.cls +3 -0
  16. package/examples/force-app/main/default/restapi/SampleRestResource.cls +7 -1
  17. package/examples/force-app/main/default/restapi/SampleRestResourceWithInnerClass.cls +24 -0
  18. package/examples/force-app/main/default/restapi/references/Reference1.cls +9 -0
  19. package/examples/force-app/main/default/restapi/references/Reference2.cls +3 -0
  20. package/examples/force-app/main/default/restapi/references/Reference3.cls +3 -0
  21. package/examples/force-app/main/default/restapi/references/Reference4.cls +3 -0
  22. package/examples/force-app/main/default/restapi/references/Reference5.cls +3 -0
  23. package/examples/force-app/main/default/restapi/references/Reference6.cls +6 -0
  24. package/lib/model/markdown-generation-util/field-declaration-util.js +2 -2
  25. package/lib/model/markdown-generation-util/field-declaration-util.js.map +1 -1
  26. package/lib/model/markdown-generation-util/method-declaration-util.js +3 -3
  27. package/lib/model/markdown-generation-util/method-declaration-util.js.map +1 -1
  28. package/lib/model/openapi/apex-doc-types.d.ts +1 -0
  29. package/lib/model/openapi/open-api-types.d.ts +6 -6
  30. package/lib/model/types-repository.d.ts +6 -1
  31. package/lib/model/types-repository.js +26 -1
  32. package/lib/model/types-repository.js.map +1 -1
  33. package/lib/test-helpers/ClassMirrorBuilder.d.ts +5 -1
  34. package/lib/test-helpers/ClassMirrorBuilder.js +12 -2
  35. package/lib/test-helpers/ClassMirrorBuilder.js.map +1 -1
  36. package/lib/test-helpers/FieldMirrorBuilder.d.ts +16 -0
  37. package/lib/test-helpers/FieldMirrorBuilder.js +48 -0
  38. package/lib/test-helpers/FieldMirrorBuilder.js.map +1 -0
  39. package/lib/test-helpers/MethodMirrorBuilder.js +4 -1
  40. package/lib/test-helpers/MethodMirrorBuilder.js.map +1 -1
  41. package/lib/transpiler/openapi/parsers/Builder.js +1 -1
  42. package/lib/transpiler/openapi/parsers/Builder.js.map +1 -1
  43. package/lib/transpiler/openapi/parsers/MethodParser.js +20 -25
  44. package/lib/transpiler/openapi/parsers/MethodParser.js.map +1 -1
  45. package/lib/transpiler/openapi/parsers/ReferenceBuilder.d.ts +19 -4
  46. package/lib/transpiler/openapi/parsers/ReferenceBuilder.js +124 -28
  47. package/lib/transpiler/openapi/parsers/ReferenceBuilder.js.map +1 -1
  48. package/lib/transpiler/openapi/parsers/ResponsesBuilder.js +5 -1
  49. package/lib/transpiler/openapi/parsers/ResponsesBuilder.js.map +1 -1
  50. package/package.json +5 -5
  51. package/src/model/__tests__/manifest-diff.spec.ts +0 -3
  52. package/src/model/markdown-generation-util/field-declaration-util.ts +5 -2
  53. package/src/model/markdown-generation-util/method-declaration-util.ts +5 -3
  54. package/src/model/openapi/apex-doc-types.ts +1 -0
  55. package/src/model/openapi/open-api-types.ts +8 -8
  56. package/src/model/types-repository.ts +36 -3
  57. package/src/test-helpers/ClassMirrorBuilder.ts +15 -3
  58. package/src/test-helpers/FieldMirrorBuilder.ts +52 -0
  59. package/src/test-helpers/MethodMirrorBuilder.ts +4 -1
  60. package/src/transpiler/openapi/parsers/Builder.ts +1 -1
  61. package/src/transpiler/openapi/parsers/MethodParser.ts +24 -26
  62. package/src/transpiler/openapi/parsers/ReferenceBuilder.ts +161 -36
  63. package/src/transpiler/openapi/parsers/ResponsesBuilder.ts +5 -1
  64. package/src/transpiler/openapi/parsers/__tests__/ParameterObjectBuilder.spec.ts +9 -5
  65. package/src/transpiler/openapi/parsers/__tests__/ReferenceBuilder.spec.ts +615 -7
  66. package/src/transpiler/openapi/parsers/__tests__/RequestBodyBuilder.spec.ts +9 -5
  67. package/src/transpiler/openapi/parsers/__tests__/ResponsesBuilder.spec.ts +9 -5
package/README.md CHANGED
@@ -120,11 +120,12 @@ The CLI supports the following parameters:
120
120
  | --sourceDir | -s | The directory location which contains your apex .cls classes. | N/A | Yes |
121
121
  | --targetDir | -t | The directory location where documentation will be generated to. | `docs` | No |
122
122
  | --recursive | -r | Whether .cls classes will be searched for recursively in the directory provided. | `true` | No |
123
- | --scope | -p | A list of scopes to document. Values should be separated by a space, e.g --scope public private | `global` | No |
124
- | --targetGenerator | -g | Define the static file generator for which the documents will be created. Currently supports: `jekyll`, `docsify`, and `plain-markdown`. | `jekyll` | No |
123
+ | --scope | -p | A list of scopes to document. Values should be separated by a space, e.g --scope public private. Note that this setting is ignored if generating an OpenApi REST specification since that looks for classes annotated with @RestResource. | `global` | No |
124
+ | --targetGenerator | -g | Define the static file generator for which the documents will be created. Currently supports: `jekyll`, `docsify`, `plain-markdown`, and `openapi`. | `jekyll` | No |
125
125
  | --indexOnly | N/A | Defines whether only the index file should be generated. | `false` | No |
126
126
  | --defaultGroupName | N/A | Defines the `@group` name to be used when a file does not specify it. | `Miscellaneous` | No |
127
- | --sanitizeHtml | N/A | When on, any special character within your ApexDocs is converted into its HTML code representation. This is specially useful when generic objects are described within the docs, e.g. "List< Foo>", "Map<Foo, Bar>" because otherwise the content within < and > would be treated as HTML tags and not shown in the output. Content in @example blocks are never sanitized. | true | No |
127
+ | --sanitizeHtml | N/A | When on, any special character within your ApexDocs is converted into its HTML code representation. This is specially useful when generic objects are described within the docs, e.g. "List< Foo>", "Map<Foo, Bar>" because otherwise the content within < and > would be treated as HTML tags and not shown in the output. Content in @example blocks are never sanitized. | `Apex REST Api` | No |
128
+ | --openApiTitle | N/A | If using "openapi" as the target generator, this allows you to specify the OpenApi title value. | true | No |
128
129
 
129
130
  ### Importing to your project
130
131
 
@@ -276,7 +277,7 @@ Apexdocs recognizes 2 different syntax when linking files:
276
277
  * @param param1 An <<ExampleClass>> instance. Can also do {@link ExampleClass}
277
278
  * @return The result of the operation.
278
279
  */
279
- public static Object class (ExampleClass param1) {
280
+ public static Object doSomething(ExampleClass param1) {}
280
281
  ```
281
282
 
282
283
  ---
@@ -285,7 +286,7 @@ Email addresses can also be inlined linked by using the `{@email EMAIL_ADDRESS}`
285
286
 
286
287
  ### HTML support
287
288
 
288
- For the most part all HTML is sanitized when the `--sanitizeHtml` flag is passed a true value (which is the default).
289
+ For the most part all HTML is sanitized when the `--sanitizeHtml` flag is passed a true value (which is the default).
289
290
  But there are some tags are allowed to have for the possibility of better
290
291
  styling long text.
291
292
 
@@ -306,8 +307,10 @@ class MyClass {
306
307
  }
307
308
  ```
308
309
 
309
- ⚠️When the `--sanitizeHtml` flag is ON, any special character between code blocks (i.e. \```, \`, or `<code>`) will also be escaped.
310
- So if you have references to Apex generic collections (Set, List, or Maps) they will not look right, as the < and > symbols will be escaped.
310
+ ⚠️When the `--sanitizeHtml` flag is ON, any special character between code blocks (i.e. \```, \`, or `<code>`) will also
311
+ be escaped.
312
+ So if you have references to Apex generic collections (Set, List, or Maps) they will not look right, as the < and >
313
+ symbols will be escaped.
311
314
  To prevent this you can turn the flag off, but be aware of the special considerations when doing this described below.
312
315
 
313
316
  ---
@@ -316,12 +319,268 @@ For full control over the output you can also turn off the `--sanitizeHtml` flag
316
319
  to have any desired HTML within your docs.
317
320
 
318
321
  ⚠️When the `--sanitizeHtml` flag is OFF, references to Apex generic collections (Set, List or Maps) can be problematic
319
- as they will be treated as an HTML tag and not displayed. For example if you have something like `@description Returns a List<String>`
322
+ as they will be treated as an HTML tag and not displayed. For example if you have something
323
+ like `@description Returns a List<String>`
320
324
  the `<String>` portion will be treated as HTML and thus not appear on the page.
321
325
 
322
- To fix this issue, when not sanitizing HTML, you should wrap any code that contain special characters that can be treated as HTML within '\`'
326
+ To fix this issue, when not sanitizing HTML, you should wrap any code that contain special characters that can be
327
+ treated as HTML within '\`'
323
328
  or within `<code>` tags.
324
329
 
330
+ ## Generating OpenApi REST Definitions
331
+
332
+ ApexDocs supports generating OpenApi 3.1.0 REST definitions based on any `@RestResource` classes in your source code.
333
+
334
+ ### Usage
335
+
336
+ To create an OpenApi specification file, run the `apexdocs-generate` and pass `openapi` to the `--targetGenerator` parameter.
337
+ When using this generator, you can also pass a custom title through the `--openApiTitle` parameter. This title will be placed
338
+ in the output file's `info.title` property, as defined by the [OpenApi documentation for the Info Object](https://spec.openapis.org/oas/v3.1.0#info-object)
339
+
340
+
341
+ ```shell
342
+ apexdocs-generate -s ./src -t docs -g openapi --openApiTitle "Custom OpenApi Title"
343
+ ```
344
+
345
+ ### How It Works
346
+
347
+ When generating an OpenApi document, since `@RestResource` classes need to be global in Apex, the `--scope` parameter will be ignored.
348
+ Instead, ApexDocs will run through all classes annotated with `@RestResource` and add it to the output OpenApi file.
349
+
350
+ Once it finishes running, a file named `openapi.json` will be created in the specified `--targetDir`.
351
+
352
+ ### Configuring What Gets Created
353
+
354
+ ApexDocs will automatically parse your source code and generate the OpenApi definition based on the HTTP related Apex
355
+ annotations (RestResource, HttpDelete, HttpGet, HttpPatch, HttpPost, HttpGet). The different HTTP annotations will be used to generate a file that complies with the [OpenApi Specification v3.1.0](https://spec.openapis.org/oas/v3.1.0)
356
+
357
+ Besides these annotations, the ApexDocs tool will also use any information provided through your code's Apexdocs, relying on
358
+ some custom annotations that are specific to generating OpenApi definitions:
359
+
360
+ * `@http-request-body`
361
+ * `@http-parameter`
362
+ * `@http-response`
363
+
364
+ #### @http-request-body
365
+
366
+ Allows you to specify the HTTP request's expected request body. It supports receiving a `description`,
367
+ whether it is `required` or not, and a `schema`, which defines the shape of the object that is expected.
368
+
369
+ 📝 Note that only one `@http-request-body` should be defined per method. If you add more than one, only
370
+ a single one will be used when generating the OpenApi definition.
371
+
372
+ The `schema` can either be a reference to another class in your source code (see the `Class References` section below)
373
+ or a fully defined custom schema (See the `Custom Schemas` section below).
374
+
375
+ Example
376
+ ```apex
377
+ /**
378
+ * @description This is a sample HTTP Post method
379
+ * @http-request-body
380
+ * description: This is an example of a request body
381
+ * required: true
382
+ * schema: ClassName
383
+ */
384
+ @HttpPost
385
+ global static void doPost() {
386
+ ///...
387
+ }
388
+ ```
389
+
390
+ 📝 Note that each parameter of this annotation is expected to be on its own line. Parameters are treated as YAML,
391
+ so spacing is important.
392
+
393
+ #### @http-parameter
394
+
395
+ Allows you to specify any HTTP parameter expected by your method. It supports receiving a `name`,
396
+ an `in` as defined by the supported [Parameter Locations](https://spec.openapis.org/oas/v3.1.0#parameter-locations),
397
+ whether it is `required` or not, a `description`, and a `schema`.
398
+
399
+ 📝 Note that you can specify as many `@http-parameter` annotations as needed.
400
+
401
+ Example
402
+ ```apex
403
+ /**
404
+ * @description This is a sample HTTP Post method
405
+ * @return A String SObject.
406
+ * @http-parameter
407
+ * name: limit
408
+ * in: query
409
+ * required: true
410
+ * description: Limits the number of items on a page
411
+ * schema:
412
+ * type: integer
413
+ * @http-parameter
414
+ * name: complex
415
+ * in: cookie
416
+ * schema: MyClassName
417
+ */
418
+ @HttpPost
419
+ global static String doPost() {
420
+ // ..
421
+ }
422
+ ```
423
+
424
+ 📝 Note that each parameter of this annotation is expected to be on its own line. Parameters are treated as YAML,
425
+ so spacing is important.
426
+
427
+ #### @http-response
428
+
429
+ Allows you to specify any HTTP response returned by your method. It supports receiving a `statusCode` with the response code,
430
+ a `description`, and a `schema`.
431
+
432
+ If no `description` is provided then one will be automatically built using the `statusCode`.
433
+
434
+ 📝 Note that you can specify as many `@http-parameter` annotations as needed.
435
+
436
+ ```apex
437
+ /**
438
+ * @description This is a sample HTTP Post method
439
+ * @return A String SObject.
440
+ * @http-response
441
+ * statusCode: 200
442
+ * schema: SuccessfulResponseClassName
443
+ * @http-response
444
+ * statusCode: 500
445
+ * description: Status Code 500 - An internal server error occurred.
446
+ * schema:
447
+ * type: string
448
+ */
449
+ @HttpPost
450
+ global static String doPost() {
451
+ // ...
452
+ }
453
+ ```
454
+
455
+ #### Class References
456
+
457
+ Whenever specifying a `schema` parameter, you can pass as a string the name of any class in your source code. This
458
+ class will be parsed by the ApexDocs tool and automatically converted to a reference in the resulting OpenApi definition.
459
+
460
+ The tool will parse the class and create a reference that complies with [Apex's support for User-Defined Types](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm#ApexRESTUserDefinedTypes)
461
+
462
+ ---
463
+
464
+ If dealing with a collection, you can also specify the name of the reference either using the `List` or `Set` syntax.
465
+
466
+ 📝 When using List or Set syntax in the `schema` of the ApexDoc `@http-*` annotation, only collections one level
467
+ deep are supported (e.g. List<List<String>> is not supported). This is only a limitation when referencing collections
468
+ on the ApexDoc `schema` property directly, and is fully supported when multi-level collections are inside of a referenced
469
+ class as part of your codebase.
470
+
471
+ Maps are not supported, as it is not possible to know which keys the map will contain, and thus it is not possible
472
+ to convert that to a valid specification. For this use case, define a Custom Schema as explained below.
473
+
474
+ ```apex
475
+ /**
476
+ * @description This is a sample HTTP Post method
477
+ * @http-request-body
478
+ * description: This is an example of a request body
479
+ * schema: List<ClassName>
480
+ */
481
+ @HttpPost
482
+ global static void doPost() {
483
+ ///...
484
+ }
485
+ ```
486
+
487
+ Inner class references are also supported, but note that you need to pass the full name of the reference,
488
+ by using the `ParentClassName.InnerClassName` syntax, even if the inner class resides on the same class as the HTTP method
489
+ referencing it.
490
+
491
+ ```apex
492
+ /**
493
+ * @description This is a sample HTTP Post method
494
+ * @http-request-body
495
+ * description: This is an example of a request body
496
+ * schema: ParentClass.InnerClass
497
+ */
498
+ @HttpPost
499
+ global static void doPost() {
500
+ ///...
501
+ }
502
+ ```
503
+
504
+ #### Custom Schemas
505
+
506
+ For any `schema` parameter in any of the HTTP ApexDocs annotations, besides specifying the name of a class, you
507
+ can also specify a custom schema definition. The schema definition can either be for a primitive type, an `object` or an `array`
508
+
509
+ **Primitives**
510
+
511
+ For primitives, you should specify the `type` and an optional `format`, as defined by the [OpenApi Specification on Data Types](https://spec.openapis.org/oas/v3.1.0#data-types)
512
+
513
+ ```apex
514
+ /**
515
+ * ...
516
+ * schema:
517
+ * type: string
518
+ * format: password
519
+ */
520
+ ```
521
+
522
+ **Objects**
523
+
524
+ To specify a custom object schema, use `object` as the `type`, and specify as many properties as follows:
525
+
526
+ ```apex
527
+ /**
528
+ * schema:
529
+ * type: object
530
+ * properties:
531
+ * id:
532
+ * type: string
533
+ * description: The super Id.
534
+ * name:
535
+ * type: string
536
+ * phone:
537
+ * type: string
538
+ * format: byte
539
+ */
540
+ ```
541
+
542
+ Properties can be defined as primitives (as explained above), other objects, or arrays (explained below)
543
+
544
+ **Arrays**
545
+
546
+ To specify a custom array schema, use `array` as the `type`, and provide an `items` definition. In `items`
547
+ you can specify the definition of any other custom type (primitives, objects, or other arrays).
548
+
549
+ ```apex
550
+ /**
551
+ * schema:
552
+ * type: array
553
+ * items:
554
+ * type: object
555
+ * properties:
556
+ * name:
557
+ * type: string
558
+ */
559
+ ```
560
+
561
+ #### SObject References
562
+
563
+ ApexDocs is not able to automatically parse SObject references, as it can with class references, as it does
564
+ not reach into your org to get existing SObject describes. Because of this, when dealing with SObject references
565
+ you should create a Custom Schema as defined above. This will also allow you to specify which specific
566
+ fields are being received or returned.
567
+
568
+
569
+ ### Considerations
570
+
571
+ Please be aware of the following when using ApexDocs to create an OpenApi definition:
572
+
573
+ * Map references are resolved as `object` with no properties, as it is not possible to know which keys the map will contain.
574
+ When using maps either create a class that better represents the shape of the object and use a Class Reference, or
575
+ define a Custom Schema in the `schema` section of the ApexDoc itself.
576
+ * Same thing when referencing SObjects, as SObject describe parsing is not supported by the ApexDocs tool. When referencing
577
+ SObjects, consider defining a Custom Schema in the `schema` section of the ApexDoc.
578
+ * ApexDoc is only able to parse through your source code, so references to other packages (namespaced classes) or any
579
+ code that lives outside your source code is not supported. Consider creating a Custom Schema for those situations.
580
+ * The return value and received parameters or your methods are currently not being considered when creating the OpenApi definition file.
581
+ Instead, use the `@http-response` ApexDoc annotation to specify the return value, and `@http-parameter` to specify any
582
+ expected parameter.
583
+
325
584
  ## Typescript
326
585
 
327
586
  ApexDocs provides all necessary type definitions.