Skip to content

Commit 2aefc60

Browse files
authored
Fix name collision with empty const/enum (#39)
1 parent c88c0a1 commit 2aefc60

File tree

8 files changed

+173
-34
lines changed

8 files changed

+173
-34
lines changed

.github/workflows/cloc.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: cloc
2+
on:
3+
pull_request:
4+
jobs:
5+
cloc:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Checkout code
9+
uses: actions/checkout@v2
10+
with:
11+
path: pr
12+
- name: Checkout base code
13+
uses: actions/checkout@v2
14+
with:
15+
ref: ${{ github.event.pull_request.base.sha }}
16+
path: base
17+
- name: Count Lines Of Code
18+
id: loc
19+
run: |
20+
curl -OL https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
21+
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
22+
OUTPUT="${OUTPUT//'%'/'%25'}"
23+
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
24+
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
25+
echo "::set-output name=diff::$OUTPUT"
26+
27+
- name: Comment Code Lines
28+
uses: marocchino/sticky-pull-request-comment@v2
29+
with:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
header: LOC
32+
message: |
33+
### Lines Of Code
34+
35+
${{ steps.loc.outputs.diff }}

.github/workflows/lint.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: lint
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
pull_request:
8+
jobs:
9+
run:
10+
runs-on: ${{ matrix.operating-system }}
11+
strategy:
12+
matrix:
13+
operating-system: [ 'ubuntu-latest' ]
14+
php-versions: [ '7.4' ]
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Cache vendor
20+
uses: actions/cache@v2
21+
with:
22+
path: |
23+
vendor
24+
key: vendor-${{ hashFiles('composer.lock') }}
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php-versions }}
30+
ini-values: post_max_size=256M, max_execution_time=180
31+
tools: composer
32+
33+
- name: Populate vendor
34+
run: '[ -e vendor ] || composer install'
35+
36+
- name: Lint
37+
run: make lint
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: test-unit-cov
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
pull_request:
8+
jobs:
9+
run:
10+
runs-on: ${{ matrix.operating-system }}
11+
strategy:
12+
matrix:
13+
operating-system: [ 'ubuntu-latest' ]
14+
php-versions: [ '7.4' ]
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Cache vendor
20+
uses: actions/cache@v2
21+
with:
22+
path: |
23+
vendor
24+
key: vendor-${{ hashFiles('composer.lock') }}
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php-versions }}
30+
ini-values: post_max_size=256M, max_execution_time=180
31+
coverage: xdebug
32+
tools: composer
33+
34+
- name: Populate vendor
35+
run: '[ -e vendor ] || composer install'
36+
37+
- name: Run Tests With Coverage
38+
run: make test-coverage && bash <(curl -s https://codecov.io/bash)

.github/workflows/test-unit.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: test-unit
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
pull_request:
8+
jobs:
9+
run:
10+
runs-on: ${{ matrix.operating-system }}
11+
strategy:
12+
matrix:
13+
operating-system: [ 'ubuntu-latest' ]
14+
php-versions: [ '5.6', '7.0', '7.1', '7.2', '7.3', '8.0' ]
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Cache vendor
20+
uses: actions/cache@v2
21+
with:
22+
path: |
23+
vendor
24+
key: vendor-${{ hashFiles('composer.lock') }}
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php-versions }}
30+
ini-values: post_max_size=256M, max_execution_time=180
31+
tools: composer
32+
33+
- name: Populate vendor
34+
run: '[ -e vendor ] || composer install'
35+
36+
- name: Run Tests
37+
run: make test

.travis.yml

Lines changed: 0 additions & 32 deletions
This file was deleted.

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.45] - 2021-07-16
8+
9+
### Fixed
10+
- Enum and const collision with type name.
11+
712
## [0.4.44] - 2021-04-20
813

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

270+
[0.4.45]: https://github.com/swaggest/go-code-builder/compare/v0.4.44...v0.4.45
265271
[0.4.44]: https://github.com/swaggest/go-code-builder/compare/v0.4.43...v0.4.44
266272
[0.4.43]: https://github.com/swaggest/go-code-builder/compare/v0.4.42...v0.4.43
267273
[0.4.42]: https://github.com/swaggest/go-code-builder/compare/v0.4.41...v0.4.42

src/JsonSchema/TypeBuilder.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,17 @@ private function processConst()
559559
GO
560560
);
561561

562+
$constName = $this->goBuilder->codeBuilder->exportableName($typeName . '_' . $this->schema->const);
563+
if ($constName === $typeName) {
564+
$constName .= 'Empty';
565+
}
566+
567+
if (isset($this->goBuilder->options->renames[$constName])) {
568+
$constName = $this->goBuilder->options->renames[$constName];
569+
}
570+
562571
$typeConstBlock->addValue(
563-
$this->goBuilder->codeBuilder->exportableName($typeName . '_' . $this->schema->const),
572+
$constName,
564573
$this->schema->const
565574
);
566575

@@ -675,6 +684,11 @@ private function processEnum(NamedType $baseType)
675684

676685
foreach ($enum as $index => $item) {
677686
$itemName = $this->goBuilder->codeBuilder->exportableName($typeName . '_' . $item);
687+
688+
if ($itemName === $typeName) {
689+
$itemName .= 'Empty';
690+
}
691+
678692
$comment = null;
679693
if (isset($enumSchemas[$index])) {
680694
$schema = $enumSchemas[$index];

tests/src/PHPUnit/JsonSchema/TypeBuilderTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ public function testEscape()
346346
"title": "Escape Issue",
347347
"enum": [
348348
"Doesn't Generate Correctly",
349-
"Does Generate"
349+
"Does Generate",
350+
""
350351
]
351352
}
352353
JSON;
@@ -366,13 +367,15 @@ public function testEscape()
366367
const (
367368
EscapeIssueDoesnTGenerateCorrectly = EscapeIssue("Doesn't Generate Correctly")
368369
EscapeIssueDoesGenerate = EscapeIssue("Does Generate")
370+
EscapeIssueEmpty = EscapeIssue("")
369371
)
370372
371373
// MarshalJSON encodes JSON.
372374
func (i EscapeIssue) MarshalJSON() ([]byte, error) {
373375
switch i {
374376
case EscapeIssueDoesnTGenerateCorrectly:
375377
case EscapeIssueDoesGenerate:
378+
case EscapeIssueEmpty:
376379
377380
default:
378381
return nil, fmt.Errorf("unexpected EscapeIssue value: %v", i)
@@ -395,6 +398,7 @@ public function testEscape()
395398
switch v {
396399
case EscapeIssueDoesnTGenerateCorrectly:
397400
case EscapeIssueDoesGenerate:
401+
case EscapeIssueEmpty:
398402
399403
default:
400404
return fmt.Errorf("unexpected EscapeIssue value: %v", v)

0 commit comments

Comments
 (0)