From 5d8905e681dfde3cb3bff22f70a9930fda6e7bc0 Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Sat, 4 Apr 2020 23:50:19 +0200 Subject: [PATCH] Add schema flag to enable/disable property generation (#31) --- API.md | 2 +- CHANGELOG.md | 8 +++ src/JsonSchema/GoBuilder.php | 8 ++- src/JsonSchema/Options.php | 6 ++ src/JsonSchema/TypeBuilder.php | 1 + .../PHPUnit/JsonSchema/TypeBuilderTest.php | 71 ++++++++++++++++++- 6 files changed, 92 insertions(+), 4 deletions(-) diff --git a/API.md b/API.md index d1c993c..e064a87 100644 --- a/API.md +++ b/API.md @@ -189,7 +189,7 @@ | Visibility | Function | |:-----------|:---------| -| public static | make([\Swaggest\GoCodeBuilder\JsonSchema\GeneratedStruct](#class-swaggestgocodebuilderjsonschemageneratedstruct) $struct) : void | +| public static | make([\Swaggest\GoCodeBuilder\JsonSchema\GeneratedStruct](#class-swaggestgocodebuilderjsonschemageneratedstruct) $struct, [\Swaggest\GoCodeBuilder\JsonSchema\Options](#class-swaggestgocodebuilderjsonschemaoptions) $options=null) : void |
diff --git a/CHANGELOG.md b/CHANGELOG.md index 73277dd..ca2612b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.4.31] - 2020-04-03 + +### Added +- Skip generation of properties with `x-generate: false`. +- Option to only generate properties with `x-generate: true`. + ## [0.4.30] - 2020-03-30 ### Added @@ -185,6 +191,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Removed unnecessary regexp dependency, #7. +[0.4.31]: https://github.com/swaggest/go-code-builder/compare/v0.4.30...v0.4.31 +[0.4.30]: https://github.com/swaggest/go-code-builder/compare/v0.4.29...v0.4.30 [0.4.29]: https://github.com/swaggest/go-code-builder/compare/v0.4.28...v0.4.29 [0.4.28]: https://github.com/swaggest/go-code-builder/compare/v0.4.27...v0.4.28 [0.4.27]: https://github.com/swaggest/go-code-builder/compare/v0.4.26...v0.4.27 diff --git a/src/JsonSchema/GoBuilder.php b/src/JsonSchema/GoBuilder.php index 744a966..c593885 100644 --- a/src/JsonSchema/GoBuilder.php +++ b/src/JsonSchema/GoBuilder.php @@ -222,6 +222,13 @@ private function makeStruct(Schema $schema, $path) if ($processProperties && $schema->properties !== null) { // Iterating over a copy (toArray) to not conflict with any other iterations in nested processings. foreach ($schema->properties->toArray() as $name => $property) { + $property = self::unboolSchema($property); + + if ($property->{TypeBuilder::X_GENERATE} === false || + ($this->options->requireXGenerate && empty($property->{TypeBuilder::X_GENERATE}))) { + continue; + } + $fieldName = $this->codeBuilder->exportableName($name); if ($this->options->trimParentFromPropertyNames) { @@ -246,7 +253,6 @@ private function makeStruct(Schema $schema, $path) $fieldName, $goPropertyType ); - $property = self::unboolSchema($property); if ($property instanceof Wrapper) { $property = $property->exportSchema(); diff --git a/src/JsonSchema/Options.php b/src/JsonSchema/Options.php index d4530cf..ad5ca98 100644 --- a/src/JsonSchema/Options.php +++ b/src/JsonSchema/Options.php @@ -102,6 +102,12 @@ class Options extends ClassStructure */ public $renames = []; + /** + * Only generate schemas that have `x-generate: true`. + * @var bool + */ + public $requireXGenerate = false; + /** * @param Properties|static $properties * @param Schema $ownerSchema diff --git a/src/JsonSchema/TypeBuilder.php b/src/JsonSchema/TypeBuilder.php index 53f2af7..0d00548 100644 --- a/src/JsonSchema/TypeBuilder.php +++ b/src/JsonSchema/TypeBuilder.php @@ -27,6 +27,7 @@ class TypeBuilder const X_GO_TYPE = 'x-go-type'; const X_OMIT_EMPTY = 'x-omitempty'; const X_NULLABLE = 'x-nullable'; + const X_GENERATE = 'x-generate'; const NULLABLE = 'nullable'; const EXAMPLES = 'examples'; const EXAMPLE = 'example'; diff --git a/tests/src/PHPUnit/JsonSchema/TypeBuilderTest.php b/tests/src/PHPUnit/JsonSchema/TypeBuilderTest.php index 741c557..7565349 100644 --- a/tests/src/PHPUnit/JsonSchema/TypeBuilderTest.php +++ b/tests/src/PHPUnit/JsonSchema/TypeBuilderTest.php @@ -144,7 +144,7 @@ function testXGoTypeGoSwaggerObject() } - function testNullable() + function testXFlags() { $prop = new Schema(); $prop->type = [Schema::STRING, Schema::NULL]; @@ -153,6 +153,7 @@ function testNullable() $propXNullable->type = Schema::STRING; $propXNullable->{TypeBuilder::X_NULLABLE} = true; $propXNullable->{TypeBuilder::X_OMIT_EMPTY} = true; + $propXNullable->{TypeBuilder::X_GENERATE} = true; $propNullable = new Schema(); $propNullable->type = Schema::STRING; @@ -162,12 +163,17 @@ function testNullable() $propKeepEmpty->type = Schema::STRING; $propKeepEmpty->{TypeBuilder::X_OMIT_EMPTY} = false; + $propSkipGenerate = new Schema(); + $propSkipGenerate->type = Schema::STRING; + $propSkipGenerate->{TypeBuilder::X_GENERATE} = false; + $obj = Schema::object(); $obj->setProperty('schema-nullable', $prop); $obj->setProperty('x-nullable', $propXNullable); $obj->setProperty('nullable', $propNullable); $obj->setProperty('regular', Schema::string()); $obj->setProperty('keep-empty', $propKeepEmpty); + $obj->setProperty('skip-generate', $propSkipGenerate); $builder = new GoBuilder(); @@ -244,7 +250,6 @@ function testNullable() , $struct->structDef->render()); - $builder = new GoBuilder(); $builder->options->defaultAdditionalProperties = false; $builder->options->enableXNullable = true; @@ -270,6 +275,68 @@ function testNullable() , $struct->structDef->render()); + $builder = new GoBuilder(); + $builder->options->defaultAdditionalProperties = false; + $builder->options->enableXNullable = true; + $builder->options->requireXGenerate = true; + $tb = new TypeBuilder($prop, '#', $builder); + $type = $tb->build(); + $this->assertEquals('*string', $type->getTypeString()); + + $this->assertEquals('Untitled1', $builder->getType($obj)->getTypeString()); + $struct = $builder->getGeneratedStruct($obj, '#'); + + $this->assertEquals(<<<'GO' +// Untitled1 structure is generated from "#". +type Untitled1 struct { + XNullable *string `json:"x-nullable,omitempty"` +} + + +GO + , $struct->structDef->render()); + + } + + public function testXGenerate() { + $schemaJson = <<<'JSON' +{ + "type": "object", + "properties": { + "id": {"type": "integer", "x-generate": true}, + "entity": {"$ref": "#/definitions/entity"} + }, + "definitions": { + "entity": { + "type": "object", + "properties": {"prop1": {"type": "string"}} + } + } +} +JSON; + $schema = Schema::import(json_decode($schemaJson)); + + $builder = new GoBuilder(); + $builder->options->defaultAdditionalProperties = false; + $builder->options->requireXGenerate = true; + $tb = new TypeBuilder($schema, '#', $builder); + $tb->build(); + + $res = ''; + foreach ($builder->getGeneratedStructs() as $generatedStruct) { + $res .= $generatedStruct->structDef->render(); + } + + $this->assertEquals(<<<'GO' +// Untitled1 structure is generated from "#". +type Untitled1 struct { + ID int64 `json:"id,omitempty"` +} + + +GO + , $res); + } } \ No newline at end of file