Skip to content

Commit

Permalink
Fix rendering of nullable+omitempty slices and maps
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Apr 30, 2020
1 parent dd9d472 commit 173b27b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ 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.38] - 2020-04-30

### Fixed
- Rendering of nullable+omitempty slices and maps.

## [0.4.37] - 2020-04-29

### Fixed
Expand Down Expand Up @@ -223,6 +228,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Removed unnecessary regexp dependency, #7.

[0.4.38]: https://github.com/swaggest/go-code-builder/compare/v0.4.37...v0.4.38
[0.4.37]: https://github.com/swaggest/go-code-builder/compare/v0.4.36...v0.4.37
[0.4.36]: https://github.com/swaggest/go-code-builder/compare/v0.4.35...v0.4.36
[0.4.35]: https://github.com/swaggest/go-code-builder/compare/v0.4.34...v0.4.35
Expand Down
13 changes: 11 additions & 2 deletions src/JsonSchema/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,13 @@ private function processArrayType()
if ($index < $itemsLen) {
} else {
if ($additionalItems instanceof Schema) {
$this->result[] = new Slice(Pointer::tryDereferenceOnce(
$sliceType = new Slice(Pointer::tryDereferenceOnce(
$this->goBuilder->getType($additionalItems, $this->path . '->' . $pathItems))
);
if ($this->nullable) {
$sliceType = new Pointer($sliceType);
}
$this->result[] = $sliceType;
}
}
}
Expand Down Expand Up @@ -353,7 +357,12 @@ private function processObjectType()
$this->getGeneratedStruct()->marshalJson->enableAdditionalProperties($structProperty);
}
} elseif ($additionalProperties instanceof Schema) {
$this->result[] = new Map(new GoType('string'), $goType);
$mapType = new Map(new GoType('string'), $goType);
if ($this->nullable) {
$mapType = new Pointer($mapType);
}

$this->result[] = $mapType;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Package entities contains generated structures.
package entities



// RequiredNullable structure is generated from "#".
type RequiredNullable struct {
Slice *[]string `json:"slice,omitempty"`
Map *map[string]string `json:"map,omitempty"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package entities

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
"github.com/swaggest/assertjson"
)

func TestRequiredNullable_MarshalJSON_roundtrip(t *testing.T) {
var (
jsonValue = []byte(`{"slice":["aeff"],"map":{"effefd":"dd"}}`)
v RequiredNullable
)

require.NoError(t, json.Unmarshal(jsonValue, &v))

marshaled, err := json.Marshal(v)
require.NoError(t, err)
require.NoError(t, json.Unmarshal(marshaled, &v))
assertjson.Equal(t, jsonValue, marshaled)
}
34 changes: 34 additions & 0 deletions tests/src/PHPUnit/JsonSchema/AdvancedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,39 @@ public function testRequiredNullable()
$this->assertSame('', $out, "Generated files changed");
}

public function testNullableOmitemptyMapSlice()
{
$schemaData = <<<'JSON'
{
"type": "object",
"properties": {
"slice": {
"type": "array",
"items": {"type":"string"},
"x-nullable": true,
"x-omitempty": true
},
"map": {
"type": "object",
"additionalProperties": {"type":"string"},
"x-nullable": true,
"x-omitempty": true
}
}
}
JSON;
$schema = Schema::import(json_decode($schemaData));
$builder = new GoBuilder();
$builder->options->defaultAdditionalProperties = false;
$builder->options->validateRequired = false;
$builder->options->ignoreRequired = false;
$builder->options->enableXNullable = true;

$path = __DIR__ . '/../../../resources/go/advanced/nullable-omitempty-map-slice';
Helper::buildEntities($builder, $schema, $path, 'RequiredNullable');

exec('git diff ' . $path, $out);
$out = implode("\n", $out);
$this->assertSame('', $out, "Generated files changed");
}
}

0 comments on commit 173b27b

Please sign in to comment.