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
90 stars 38 forks source link

Required and nullable is ignored #131

Closed JohnGalt1717 closed 6 months ago

JohnGalt1717 commented 10 months ago

Consider the following definition:

            "ArticleDto": {
                "required": [
                    "OriginalStoryBrokeOn",
                    "CreatedOn",
                    "UpdatedOn",
                    "IsActive",
                    "Tags",
                    "Categories",
                    "ConfidenceScore"
                ],
                "type": "object",
                "properties": {
                    "id": {
                        "type": "string",
                        "format": "uuid",
                        "nullable": true
                    },
                    "title": {
                        "type": "string"
                    },
                    "synopsis": {
                        "type": "string"
                    },
                    "originalStoryBrokeOn": {
                        "type": "string",
                        "format": "date-time"
                    },
                    "createdOn": {
                        "type": "string",
                        "format": "date-time"
                    },
                    "updatedOn": {
                        "type": "string",
                        "format": "date-time"
                    },
                    "body": {
                        "type": "string"
                    },
                    "isActive": {
                        "type": "boolean"
                    },
                    "tags": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        }
                    },
                    "categories": {
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/ArticleCategories"
                        }
                    },
                    "country": {
                        "type": "string",
                        "nullable": true
                    },
                    "stateProvince": {
                        "type": "string",
                        "nullable": true
                    },
                    "city": {
                        "type": "string",
                        "nullable": true
                    },
                    "slug": {
                        "type": "string"
                    },
                    "confidenceScore": {
                        "type": "number",
                        "format": "float"
                    }
                },
                "additionalProperties": false
            },

It clearly defines what fields are required and nullable, but the generation doesn't use these, so everything is nullable when it really isn't.

clragon commented 9 months ago

I also experience this issue.

Here is a minimal reproducible sample:

{
  "openapi": "3.0.3",
  "info": {
    "title": "Parser Test",
    "description": "Parser Test",
    "version": "1.0.0"
  },
  "externalDocs": {
    "url": "/"
  },
  "servers": [],
  "tags": [],
  "paths": {},
  "components": {
    "schemas": {
      "User": {
        "type": "object",
        "properties": {
          "a": {
            "type": "string",
            "nullable": false
          }
        }
      }
    },
    "examples": {}
  }
}

This snippet generates the following dart code:

// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint

import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable()
class User {
  const User({
    this.a,
  });

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

  final String? a;

  Map<String, Object?> toJson() => _$UserToJson(this);
}

But for my understanding, this should actually mark a as String (and therefore required) instead of String?.

Darkildo commented 6 months ago

Can I somehow help solve the problem? Or someone is already doing this. Just a very critical thing...

JohnGalt1717 commented 6 months ago

I already have the code and offered to do a pull request. I didn’t get a response.

Carapacik commented 6 months ago

Yes, I am work on it

Carapacik commented 6 months ago

@JohnGalt1717 @Darkildo Check branch 1.15

  swagger_parser:
    git:
      url: https://github.com/Carapacik/swagger_parser
      ref: 1.15
      path: swagger_parser
clragon commented 6 months ago

Turns out, I was just writing my swagger docs incorrectly and required_by_default: false is actually just vastly more desirable. I apologize for that.

A swagger doc were all required parameters are annotated with required: true is much easier to generate that one which doesnt do that but assumes all parameters to be required.

Strangely enough, required_by_default: false seems to not actually turn off the required part for parameters?