Skip to content

Commit 0cbe58c

Browse files
authored
Add JSDoc generator (#25)
1 parent 2b509ed commit 0cbe58c

File tree

11 files changed

+543
-35
lines changed

11 files changed

+543
-35
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.8.4] - 2021-04-07
8+
9+
### Added
10+
- Generation of JSDoc type definitions from JSON Schema with `gen-jsdoc`.
11+
712
## [1.8.3] - 2020-12-14
813

914
### Fixed

README.md

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
<img align="right" width="100px" height="100px" alt="Swiss Knife" src="./knife.svg">
44

5-
A CLI app to find unordered diff between two `JSON` documents (based on [`swaggest/json-diff`](https://github.com/swaggest/json-diff)), generate JSON Schema and Go/PHP code, pretty print, minify, yaml convert, etc....
5+
A CLI app to find unordered diff between two `JSON` documents (based
6+
on [`swaggest/json-diff`](https://github.com/swaggest/json-diff)), generate JSON Schema and Go/PHP code, pretty print,
7+
minify, yaml convert, etc....
68

79
[![Build Status](https://travis-ci.org/swaggest/json-cli.svg?branch=master)](https://travis-ci.org/swaggest/json-cli)
810
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/swaggest/json-cli/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/swaggest/json-cli/?branch=master)
@@ -14,17 +16,20 @@ A CLI app to find unordered diff between two `JSON` documents (based on [`swagge
1416

1517
## Purpose
1618

17-
* To simplify changes review between two `JSON` files you can use a standard `diff` tool on rearranged pretty-printed `JSON`.
18-
* To detect breaking changes by analyzing removals and changes from original `JSON`.
19-
* To keep original order of object sets (for example `swagger.json` [parameters](https://swagger.io/docs/specification/describing-parameters/) list).
20-
* To make and apply JSON Patches, specified in [RFC 6902](http://tools.ietf.org/html/rfc6902) from the IETF.
21-
* To convert between YAML/JSON/PHP serialization.
22-
* To resolve `JSON Pointer` to data.
23-
* To resolve `JSON Pointer` to file position.
24-
* To validate JSON data against [`JSON Schema`](http://json-schema.org/).
25-
* To [generate or update](#buildschema) JSON Schema with instance value(s).
26-
* To [render](#gengo) `JSON Schema` as [`Go`](http://golang.org/) structure.
27-
* To [render](#genphp) `JSON Schema` as `PHP` classes.
19+
* To simplify changes review between two `JSON` files you can use a standard `diff` tool on rearranged
20+
pretty-printed `JSON`.
21+
* To detect breaking changes by analyzing removals and changes from original `JSON`.
22+
* To keep original order of object sets (for
23+
example `swagger.json` [parameters](https://swagger.io/docs/specification/describing-parameters/) list).
24+
* To make and apply JSON Patches, specified in [RFC 6902](http://tools.ietf.org/html/rfc6902) from the IETF.
25+
* To convert between YAML/JSON/PHP serialization.
26+
* To resolve `JSON Pointer` to data.
27+
* To resolve `JSON Pointer` to file position.
28+
* To validate JSON data against [`JSON Schema`](http://json-schema.org/).
29+
* To [generate or update](#buildschema) JSON Schema with instance value(s).
30+
* To [render](#gengo) `JSON Schema` as [`Go`](http://golang.org/) structure.
31+
* To [render](#genphp) `JSON Schema` as `PHP` classes.
32+
* To [render](#genjsdoc) `JSON Schema` as `JSDoc` type definitions.
2833

2934
## Installation
3035

@@ -60,6 +65,7 @@ Usage:
6065
```
6166

6267
Input paths can be .json/.yaml/.yml/.serialized files, file format is detected by file extension:
68+
6369
* `.json` JSON
6470
* `.yaml`, `.yml` YAML
6571
* `.serialized` PHP serialization format
@@ -178,7 +184,6 @@ json-cli rearrange tests/assets/original.json tests/assets/new.json --rearrange-
178184
> "key5": "wat"
179185
```
180186

181-
182187
#### Show difference between two JSON documents
183188

184189
```
@@ -266,7 +271,8 @@ Options:
266271
--eol Add line break to the output
267272
```
268273

269-
Bash command to minify all JSON files in current directory.
274+
Bash command to minify all JSON files in current directory.
275+
270276
```
271277
for f in *.json; do json-cli minify $f --output $f; done
272278
```
@@ -348,6 +354,7 @@ Usage:
348354
```
349355

350356
Example:
357+
351358
```
352359
json-cli validate-schema tests/assets/sample-data.json tests/assets/sample-schema.json
353360
Data is invalid
@@ -386,6 +393,7 @@ Options:
386393
```
387394

388395
Basic example:
396+
389397
```
390398
json-cli build-schema tests/assets/original.json
391399
@@ -413,7 +421,7 @@ Usage:
413421
schema Path to JSON schema file
414422
415423
Options:
416-
--ptr-in-schema <ptrInSchema...> JSON pointers to structure in in root schema, default #
424+
--ptr-in-schema <ptrInSchema...> JSON pointers to structure in root schema, default #
417425
--def-ptr <defPtr...> Definitions pointers to strip from symbol names, default #/definitions
418426
--patches <patches...> JSON patches to apply to schema file before processing, merge patches are also supported
419427
--output <output> Path to output .go file, STDOUT is used by default
@@ -440,6 +448,7 @@ Example:
440448
```
441449
json-cli gen-go http://json-schema.org/learn/examples/address.schema.json
442450
```
451+
443452
```
444453
// Code generated by github.com/swaggest/json-cli v1.6.3, DO NOT EDIT.
445454
@@ -462,7 +471,7 @@ type Structure struct {
462471
}
463472
```
464473

465-
Advanced example:
474+
Advanced example:
466475

467476
```
468477
json-cli gen-go "https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0-rc1/examples/1.2.0/streetlights.yml" \
@@ -555,7 +564,7 @@ Usage:
555564
schema Path to JSON schema file
556565
557566
Options:
558-
--ptr-in-schema <ptrInSchema...> JSON pointers to structure in in root schema, default #
567+
--ptr-in-schema <ptrInSchema...> JSON pointers to structure in root schema, default #
559568
--def-ptr <defPtr...> Definitions pointers to strip from symbol names, default #/definitions
560569
--patches <patches...> JSON patches to apply to schema file before processing, merge patches are also supported
561570
--ns <ns> Namespace to use for generated classes, example \MyClasses
@@ -568,7 +577,7 @@ Options:
568577
--build-additional-properties-accessors Build accessors for additionalProperties
569578
```
570579

571-
Advanced example:
580+
Advanced example:
572581

573582
```
574583
mkdir ./StreetLights
@@ -698,3 +707,44 @@ class TurnOnOffPayload extends ClassStructure
698707
}
699708
}
700709
```
710+
711+
#### <a name="genjsdoc"></a> Generate `JSDoc` type definitions from `JSON Schema`.
712+
713+
```
714+
v1.8.4 json-cli gen-jsdoc
715+
JSON CLI tool, https://github.com/swaggest/json-cli
716+
Generate JSDoc code from JSON schema
717+
Usage:
718+
json-cli gen-jsdoc <schema>
719+
schema Path to JSON schema file
720+
721+
Options:
722+
--ptr-in-schema <ptrInSchema...> JSON pointers to structure in root schema, default #
723+
--def-ptr <defPtr...> Definitions pointers to strip from symbol names, default #/definitions
724+
--patches <patches...> JSON patches to apply to schema file before processing, merge patches are also supported
725+
```
726+
727+
Example:
728+
729+
```
730+
json-cli gen-jsdoc "https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0-rc1/examples/1.2.0/streetlights.yml" \
731+
--ptr-in-schema "#/components/messages/lightMeasured/payload" "#/components/messages/turnOnOff/payload" \
732+
--def-ptr "#/components/schemas"
733+
```
734+
735+
```
736+
/**
737+
* @typedef ComponentsMessagesLightMeasuredPayload
738+
* @type {object}
739+
* @property {number} lumens - Light intensity measured in lumens.
740+
* @property {string} sentAt - Date and time when the message was sent.
741+
*/
742+
743+
/**
744+
* @typedef ComponentsMessagesTurnOnOffPayload
745+
* @type {object}
746+
* @property {string} command - Whether to turn on or off the light.
747+
* @property {string} sentAt - Date and time when the message was sent.
748+
*/
749+
750+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"salsify/json-streaming-parser": "^7.0",
1818
"swaggest/json-schema": "^0.12.21",
1919
"swaggest/go-code-builder": "^0.4.42",
20-
"swaggest/php-code-builder": "^0.2.8",
20+
"swaggest/php-code-builder": "^0.2.29",
2121
"swaggest/code-builder": "^0.3.2",
2222
"swaggest/json-schema-maker": "^0.3.4"
2323
},

composer.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class App extends Command\Application
99
{
10-
public static $ver = 'v1.8.3';
10+
public static $ver = 'v1.8.4';
1111

1212
public $diff;
1313
public $apply;
@@ -21,6 +21,7 @@ class App extends Command\Application
2121
public $validateSchema;
2222
public $genGo;
2323
public $genPhp;
24+
public $genJSDoc;
2425
public $buildSchema;
2526

2627
/**
@@ -45,6 +46,7 @@ static function setUpCommands(Definition $definition, $commandDefinitions)
4546
$commandDefinitions->validateSchema = ValidateSchema::definition();
4647
$commandDefinitions->genGo = GenGo::definition();
4748
$commandDefinitions->genPhp = GenPhp::definition();
49+
$commandDefinitions->genJSDoc = GenJSDoc::definition();
4850
$commandDefinitions->buildSchema = BuildSchema::definition();
4951
}
5052
}

src/Base.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use Swaggest\JsonCli\Json\LoadFile;
66
use Swaggest\JsonCli\JsonSchema\ResolverMux;
77
use Swaggest\JsonDiff\Exception;
8-
use Swaggest\JsonDiff\JsonMergePatch;
9-
use Swaggest\JsonDiff\JsonPatch;
108
use Swaggest\JsonSchema\Context;
119
use Swaggest\JsonSchema\RemoteRef\BasicFetcher;
1210
use Swaggest\JsonSchema\RemoteRef\Preloaded;
@@ -116,7 +114,7 @@ protected static function setupGenOptions(Command\Definition $definition, $optio
116114
->setDescription('Path to JSON schema file')->setIsUnnamed()->setIsRequired();
117115

118116
$options->ptrInSchema = Command\Option::create()->setType()->setIsVariadic()
119-
->setDescription('JSON pointers to structure in in root schema, default #');
117+
->setDescription('JSON pointers to structure in root schema, default #');
120118

121119
$options->defPtr = Command\Option::create()->setType()->setIsVariadic()
122120
->setDescription('Definitions pointers to strip from symbol names, default #/definitions');
@@ -139,14 +137,20 @@ protected function loadSchema(&$skipRoot, &$baseName)
139137

140138
$dataValue = $this->loadFile();
141139
$data = $dataValue;
142-
140+
143141
if (!empty($this->ptrInSchema)) {
144142
$baseName = basename($this->schema);
145143
$skipRoot = true;
146144
$preloaded = new Preloaded();
147145
$preloaded->setSchemaData($baseName, $dataValue);
148146
$resolver->resolvers[] = $preloaded;
149147
$data = new \stdClass();
148+
149+
foreach ($this->defPtr as $defPtr) {
150+
$this->defPtr[] = $baseName . $defPtr;
151+
}
152+
$this->defPtr[] = $baseName;
153+
150154
foreach ($this->ptrInSchema as $i => $ptr) {
151155
$data->oneOf[$i] = (object)[Schema::PROP_REF => $baseName . $ptr];
152156
}

src/GenJSDoc.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Swaggest\JsonCli;
4+
5+
use Swaggest\JsonCli\GenPhp\BuilderOptions;
6+
use Swaggest\JsonSchema\Schema;
7+
use Swaggest\PhpCodeBuilder\JSDoc\TypeBuilder;
8+
use Yaoi\Command;
9+
10+
class GenJSDoc extends Base
11+
{
12+
use BuilderOptions;
13+
14+
/**
15+
* @param Command\Definition $definition
16+
* @param \stdClass|static $options
17+
*/
18+
public static function setUpDefinition(Command\Definition $definition, $options)
19+
{
20+
$definition->description = 'Generate JSDoc code from JSON schema';
21+
Base::setupGenOptions($definition, $options);
22+
}
23+
24+
25+
public function performAction()
26+
{
27+
try {
28+
$skipRoot = false;
29+
$baseName = null;
30+
$schema = $this->loadSchema($skipRoot, $baseName);
31+
32+
$jb = new TypeBuilder();
33+
$jb->trimNamePrefix = $this->defPtr;
34+
35+
if (!$schema instanceof Schema) {
36+
$this->response->error('failed to assert Schema type, ' . get_class($schema) . ' received');
37+
throw new ExitCode('', 1);
38+
}
39+
40+
$jb->getTypeString($schema);
41+
42+
if ($this->output) {
43+
if (!file_exists(dirname($this->output))) {
44+
$this->response->error('Destination directory does not exist, please create: ' . dirname($this->output));
45+
throw new ExitCode('', 1);
46+
}
47+
file_put_contents($this->output, $jb->file);
48+
49+
} else {
50+
echo $jb->file;
51+
}
52+
} catch (\Exception $e) {
53+
$this->response->error($e->getMessage());
54+
throw new ExitCode('', 1);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)