Carapacik / swagger_parser

Dart package that takes an OpenApi definition file and generates REST clients based on retrofit and data classes for your project.
https://pub.dev/packages/swagger_parser
MIT License
94 stars 43 forks source link

Question: required, nullable combinations #76

Closed JasCodes closed 1 year ago

JasCodes commented 1 year ago

All all gereated fields should be optional not required

Schema components->schema

"FindAllStudentsDto": {
                "type": "object",
                "properties": {
                    "page": {
                        "type": "number",
                        "default": 1
                    },
                    "from": {
                        "type": "string"
                    },
                    "to": {
                        "type": "string"
                    },
                    "limit": {
                        "type": "number",
                        "default": 50
                    },
                    "name": {
                        "type": "string"
                    },
                    "departments": {
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/IdDto"
                        }
                    },
                    "subjects": {
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/IdDto"
                        }
                    },
                    "classGroups": {
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/IdDto"
                        }
                    },
                    "yearGroups": {
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/IdDto"
                        }
                    },
                    "formGroups": {
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/IdDto"
                        }
                    },
                    "ids": {
                        "type": "array",
                        "items": {
                            "type": "number"
                        }
                    }
                }
            },

Generated Code

import 'package:json_annotation/json_annotation.dart';

import 'id_dto.dart';

part 'find_all_students_dto.g.dart';

@JsonSerializable()
class FindAllStudentsDto {
  const FindAllStudentsDto({
    required this.from,
    required this.to,
    required this.name,
    required this.departments,
    required this.subjects,
    required this.classGroups,
    required this.yearGroups,
    required this.formGroups,
    required this.ids,
    required this.withExamAndHomeworkStats,
    this.page = 1,
    this.limit = 50,
  });

  factory FindAllStudentsDto.fromJson(Map<String, Object?> json) => _$FindAllStudentsDtoFromJson(json);

  final String from;
  final String to;
  final num page;
  final num limit;
  final String name;
  final List<IdDto> departments;
  final List<IdDto> subjects;
  final List<IdDto> classGroups;
  final List<IdDto> yearGroups;
  final List<IdDto> formGroups;
  final List<num> ids;
  final bool withExamAndHomeworkStats;

  Map<String, Object?> toJson() => _$FindAllStudentsDtoToJson(this);
}
Carapacik commented 1 year ago

This is correct behavior, because the nullable field is not set. Therefore required must be.

JasCodes commented 1 year ago

@Carapacik Mate, you might be not correct here, as per openapi standards

If both nullable and required are not specified in the schema, it means that:

Required: The field is not required by default. This means that when a request is made, the field does not necessarily have to be present in the request body.

Nullable: The field is not nullable by default. This means that the field, if provided, should not be null.

During network call if the key is marked required it should be always present, but if key is nullable that means key can be still present with null value

"name": {
    "type": "string",
    "nullable": true
}

then

Then, it is acceptable to have "name": null in your API request.

Carapacik commented 1 year ago

That is, you propose to make the required false and nullable true by default?

JasCodes commented 1 year ago

yes. in effect Mate, a field can be nullable and required at the same time, so its subtle. But as per openapi standard, during request key can be not passed and server should handle that unless its marked required. In that case error should be thrown.

Carapacik commented 1 year ago

I'll think about it in the next few days.