Skip to content

Commit

Permalink
Add schema flag to enable/disable property generation (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Apr 4, 2020
1 parent 9781ddf commit 5d8905e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@

| Visibility | Function |
|:-----------|:---------|
| public static | <strong>make(</strong><em>[\Swaggest\GoCodeBuilder\JsonSchema\GeneratedStruct](#class-swaggestgocodebuilderjsonschemageneratedstruct)</em> <strong>$struct</strong>)</strong> : <em>void</em> |
| public static | <strong>make(</strong><em>[\Swaggest\GoCodeBuilder\JsonSchema\GeneratedStruct](#class-swaggestgocodebuilderjsonschemageneratedstruct)</em> <strong>$struct</strong>, <em>[\Swaggest\GoCodeBuilder\JsonSchema\Options](#class-swaggestgocodebuilderjsonschemaoptions)</em> <strong>$options=null</strong>)</strong> : <em>void</em> |

<hr />

Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/JsonSchema/GoBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -246,7 +253,6 @@ private function makeStruct(Schema $schema, $path)
$fieldName,
$goPropertyType
);
$property = self::unboolSchema($property);

if ($property instanceof Wrapper) {
$property = $property->exportSchema();
Expand Down
6 changes: 6 additions & 0 deletions src/JsonSchema/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/JsonSchema/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
71 changes: 69 additions & 2 deletions tests/src/PHPUnit/JsonSchema/TypeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function testXGoTypeGoSwaggerObject()
}


function testNullable()
function testXFlags()
{
$prop = new Schema();
$prop->type = [Schema::STRING, Schema::NULL];
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -244,7 +250,6 @@ function testNullable()
, $struct->structDef->render());



$builder = new GoBuilder();
$builder->options->defaultAdditionalProperties = false;
$builder->options->enableXNullable = true;
Expand All @@ -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);

}

}

0 comments on commit 5d8905e

Please sign in to comment.