Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix #52

Merged
merged 3 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions .github/ISSUE_TEMPLATE/bug_report.md

This file was deleted.

57 changes: 57 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: 🐞 Bug report
description: File a bug/issue to help us improve.
title: "[BUG] <title>"
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 8 additions & 6 deletions src/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}') ` : '';
};

/**
Expand Down Expand Up @@ -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';
Expand All @@ -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 {
Expand Down