diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index d70ed59..0000000 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -name: Bug report -about: Create a report to help us improve -title: '' -labels: '' -assignees: '' - ---- - -**Describe the bug** -A clear and concise description of what the bug is. - -**To Reproduce** -Steps to reproduce the behavior: -1. Go to '...' -2. Click on '....' -3. Scroll down to '....' -4. See error - -**Expected behavior** -A clear and concise description of what you expected to happen. - -**Screenshots** -If applicable, add screenshots to help explain your problem. - -**Version** -version you faced on - -**Json** -Json you tried to convert - -**Additional context** -Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/bug_report.yaml b/.github/ISSUE_TEMPLATE/bug_report.yaml new file mode 100644 index 0000000..e921b49 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.yaml @@ -0,0 +1,57 @@ +name: 🐞 Bug report +description: File a bug/issue to help us improve. +title: "[BUG] " +labels: [Bug, Needs, Triage, Enhancement, Question] +body: + - type: checkboxes + attributes: + label: Is there an existing issue for this? + description: Please search to see if an issue already exists for the bug you encountered. + options: + - label: I have searched the existing issues + required: true + - type: textarea + attributes: + label: Current Behavior + description: A concise description of what you're experiencing. + validations: + required: false + - type: textarea + attributes: + label: Expected Behavior + description: A concise description of what you expected to happen. + validations: + required: false + - type: textarea + attributes: + label: Steps To Reproduce + description: Steps to reproduce the behavior. + placeholder: | + 1. In this environment... + 2. With this config... + 3. Run '...' + 4. See error... + validations: + required: false + - type: textarea + attributes: + label: Version + description: Version you faced on. + render: markdown + validations: + required: false + - type: textarea + id: logs + attributes: + label: Relevant JSON syntax + description: Please copy and paste JSON you tried to convert. This will be automatically formatted into code, so no need for backticks. + render: shell + - type: textarea + attributes: + label: Anything else? + description: | + Links? References? Anything that will give us more context about the issue you are encountering! + + Tip: You can attach images or log files by clicking this area to highlight it and then dragging files in. + validations: + required: false diff --git a/CHANGELOG.md b/CHANGELOG.md index 202e906..98b03c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 3.3.4 + + - Fixed JSON annotation key bug. + ## 3.3.3 - Fixed JSON annotation key for Freezed and JSON serializable, ex: `@JsonKey(name: user_id)`. JSON key annotation will be added only when needed. This provides a cleaner code syntax. diff --git a/package-lock.json b/package-lock.json index a0af388..7c5d7f2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "json-to-dart", - "version": "3.3.3", + "version": "3.3.4", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 098d16a..65f3525 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "json-to-dart", "displayName": "Json to Dart Model", "description": "Extension convert Json to Dart Model class", - "version": "3.3.3", + "version": "3.3.4", "publisher": "hirantha", "icon": "icon.png", "engines": { diff --git a/src/syntax.ts b/src/syntax.ts index 4d91a6b..514684f 100644 --- a/src/syntax.ts +++ b/src/syntax.ts @@ -66,8 +66,8 @@ export const printLine = (print: string, lines = 0, tabs = 0): string => { * Adds JSON annotation only if needed for Freezed and JSON serializable. * @param {string} jsonKey a raw JSON key. */ -const jsonKeyAnnotation = (jsonKey: string): string => { - return jsonKey.includes('_') ? `@JsonKey(name: '${jsonKey}') ` : ''; +const jsonKeyAnnotation = (name: string, jsonKey: string): string => { + return name !== jsonKey ? `@JsonKey(name: '${jsonKey}') ` : ''; }; /** @@ -593,9 +593,10 @@ export class ClassDefinition { for (const f of [...this.fields.values()]) { const fieldName = f.getName(this._privateFields); + const jsonKey = jsonKeyAnnotation(f.name, f.jsonKey); - if (f.jsonKey.includes('_')) { - sb += '\t' + jsonKeyAnnotation(f.jsonKey) + '\n'; + if (jsonKey.length) { + sb += '\t' + jsonKey + '\n'; } sb += '\t' + final + this.addType(f, input) + ` ${fieldName};` + '\n'; @@ -620,13 +621,14 @@ export class ClassDefinition { sb += printLine(`${input.nullSafety ? '' : 'abstract '}class ${this.name} with `, 1); sb += printLine(`_$${this.name} {`); sb += printLine(`factory ${this.name}({`, 1, 1); - for (var [name, typeDef] of this.fields) { + for (const typeDef of [...this.fields.values()]) { const optional = 'optional' + pascalCase(typeDef.name); const fieldName = typeDef.getName(this._privateFields); - const jsonKey = jsonKeyAnnotation(typeDef.jsonKey); + const jsonKey = jsonKeyAnnotation(typeDef.name, typeDef.jsonKey); const defaultVal = defaultValue(typeDef, input.nullSafety, true); const required = requiredValue(typeDef.required, input.nullSafety); sb += printLine(jsonKey + required + defaultVal, 1, 2); + if (typeDef.isDate && typeDef.defaultValue && !typeDef.isList) { sb += printLine(`${this.addType(typeDef, input, input.nullSafety)} ${optional},`); } else {