Skip to content

Commit c88c0a1

Browse files
authored
Fix missing imports in generated code (#38)
1 parent b602663 commit c88c0a1

File tree

4 files changed

+122
-7
lines changed

4 files changed

+122
-7
lines changed

CHANGELOG.md

Lines changed: 6 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+
## [0.4.44] - 2021-04-20
8+
9+
### Fixed
10+
- Missing imports in generated code.
11+
712
## [0.4.43] - 2021-04-20
813

914
### Fixed
@@ -257,6 +262,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
257262
### Fixed
258263
- Removed unnecessary regexp dependency, #7.
259264

265+
[0.4.44]: https://github.com/swaggest/go-code-builder/compare/v0.4.43...v0.4.44
260266
[0.4.43]: https://github.com/swaggest/go-code-builder/compare/v0.4.42...v0.4.43
261267
[0.4.42]: https://github.com/swaggest/go-code-builder/compare/v0.4.41...v0.4.42
262268
[0.4.41]: https://github.com/swaggest/go-code-builder/compare/v0.4.40...v0.4.41

src/JsonSchema/MarshalEnum.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ private function renderConstUnmarshal()
7777
return '';
7878
}
7979

80-
$this->code->imports()->addByName('encoding/json');
80+
$this->code->imports()
81+
->addByName('encoding/json')
82+
->addByName('fmt');
8183
return <<<GO
8284
// UnmarshalJSON decodes JSON.
8385
func (i *:type) UnmarshalJSON(data []byte) error {
@@ -143,7 +145,9 @@ private function renderUnmarshal()
143145
return '';
144146
}
145147

146-
$this->code->imports()->addByName('encoding/json');
148+
$this->code->imports()
149+
->addByName('encoding/json')
150+
->addByName('fmt');
147151
return <<<GO
148152
// UnmarshalJSON decodes JSON.
149153
func (i *:type) UnmarshalJSON(data []byte) error {

src/JsonSchema/MarshalJson.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ private function renderUnmarshal()
387387

388388
// Additional properties forbidden.
389389
if ($this->additionalPropertiesEnabled === false) {
390+
$this->code->imports()->addByName('fmt');
390391
$mapUnmarshal .= <<<'GO'
391392
392393
if len(rawMap) != 0 {
@@ -575,6 +576,7 @@ private function renderOneOfUnmarshal()
575576
GO;
576577
}
577578

579+
$this->code->imports()->addByName('fmt');
578580
$result .= <<<'GO'
579581
580582
@@ -596,10 +598,6 @@ private function renderAnyOfUnmarshal()
596598
return $result;
597599
}
598600

599-
$this->code->imports()
600-
->addByName('encoding/json')
601-
->addByName('fmt');
602-
603601
$count = count($this->someOf[$kind]);
604602
$result .= <<<GO
605603
@@ -609,6 +607,7 @@ private function renderAnyOfUnmarshal()
609607
610608
GO;
611609

610+
$this->code->imports()->addByName('encoding/json');
612611
foreach ($this->someOf[$kind] as $i => $propertyName) {
613612
$result .= <<<GO
614613
@@ -623,6 +622,7 @@ private function renderAnyOfUnmarshal()
623622
GO;
624623
}
625624

625+
$this->code->imports()->addByName('fmt');
626626
$result .= <<<'GO'
627627
628628
@@ -688,6 +688,7 @@ private function renderNot()
688688
return '';
689689
}
690690

691+
$this->code->imports()->addByName('errors');
691692
return <<<GO
692693
693694
var not {$this->not->render()}

tests/src/PHPUnit/JsonSchema/TypeBuilderTest.php

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Swaggest\GoCodeBuilder\JsonSchema\GoBuilder;
77
use Swaggest\GoCodeBuilder\JsonSchema\TypeBuilder;
8+
use Swaggest\GoCodeBuilder\Templates\GoFile;
89
use Swaggest\JsonSchema\Context;
910
use Swaggest\JsonSchema\RemoteRef\Preloaded;
1011
use Swaggest\JsonSchema\Schema;
@@ -449,7 +450,6 @@ public function testTypeNaming()
449450
$res .= $generatedStruct->structDef->render();
450451
}
451452

452-
453453
$this->assertEquals(<<<'GO'
454454
// Another is an enum type.
455455
type Another string
@@ -570,4 +570,108 @@ public function testTypeNaming()
570570

571571
}
572572

573+
public function testMissingImport()
574+
{
575+
$schemaJson = <<<'JSON'
576+
{
577+
"type": "object",
578+
"$schema": "http://json-schema.org/draft-04/schema#",
579+
"additionalProperties": false,
580+
"properties": {
581+
"version": {
582+
"type": "string"
583+
}
584+
},
585+
"required": [
586+
"version"
587+
]
588+
}
589+
JSON;
590+
$schema = Schema::import(json_decode($schemaJson));
591+
592+
$builder = new GoBuilder();
593+
$tb = new TypeBuilder($schema, '#', $builder);
594+
$tb->build();
595+
596+
foreach ($builder->getGeneratedStructs() as $generatedStruct) {
597+
$builder->getCode()->addSnippet($generatedStruct->structDef);
598+
}
599+
600+
$res = new GoFile('foo');
601+
$res->setCode($builder->getCode());
602+
603+
$this->assertEquals(<<<'GO'
604+
// Code generated by github.com/swaggest/go-code-builder, DO NOT EDIT.
605+
606+
package foo
607+
608+
import (
609+
"encoding/json"
610+
"errors"
611+
"fmt"
612+
)
613+
614+
// Untitled1 structure is generated from "#".
615+
type Untitled1 struct {
616+
Version string `json:"version"` // Required.
617+
}
618+
619+
type marshalUntitled1 Untitled1
620+
621+
var knownKeysUntitled1 = []string{
622+
"version",
623+
}
624+
625+
var requireKeysUntitled1 = []string{
626+
"version",
627+
}
628+
629+
// UnmarshalJSON decodes JSON.
630+
func (u *Untitled1) UnmarshalJSON(data []byte) error {
631+
var err error
632+
633+
mu := marshalUntitled1(*u)
634+
635+
err = json.Unmarshal(data, &mu)
636+
if err != nil {
637+
return err
638+
}
639+
640+
var rawMap map[string]json.RawMessage
641+
642+
err = json.Unmarshal(data, &rawMap)
643+
if err != nil {
644+
rawMap = nil
645+
}
646+
647+
for _, key := range requireKeysUntitled1 {
648+
if _, found := rawMap[key]; !found {
649+
return errors.New("required key missing: " + key)
650+
}
651+
}
652+
653+
for _, key := range knownKeysUntitled1 {
654+
delete(rawMap, key)
655+
}
656+
657+
if len(rawMap) != 0 {
658+
offendingKeys := make([]string, 0, len(rawMap))
659+
660+
for key := range rawMap {
661+
offendingKeys = append(offendingKeys, key)
662+
}
663+
664+
return fmt.Errorf("additional properties not allowed in Untitled1: %v", offendingKeys)
665+
}
666+
667+
*u = Untitled1(mu)
668+
669+
return nil
670+
}
671+
672+
GO
673+
, $res->render());
674+
675+
}
676+
573677
}

0 commit comments

Comments
 (0)