Coder Social home page Coder Social logo

Comments (5)

 avatar commented on August 14, 2024

Perhaps that would be a good idea. There is another option though.

If you use json.net, you can add the JsonPropertyAttribute to a property with the Required property to Required.Always (see http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonPropertyAttribute_Required.htm).

This will remove the? during code generation.

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

Yes, using JsonPropertyAttribute.Required or the RequiredAttribute removes the ?. But the semantics of ? is not nullability but optionality... not-nullability cannot be expressed in TS... The problem is, that there must be lots of required attributes just to have safer TS...

from njsonschema.

 avatar commented on August 14, 2024

I agree using the attribute introduces alot of bloat. The eaay solution would be to remove the optionality for the generated interfaces.

The semantics of an interface are that a class should mimimally implement its defined members, if they are all optional then you could wonder why use an interface anyway.

So yes, perhaps removing them is best.

from njsonschema.

RicoSuter avatar RicoSuter commented on August 14, 2024

Discussion here: https://gitter.im/NSwag/NSwag

from njsonschema.

simeyla avatar simeyla commented on August 14, 2024

What are you talking about 'all TS properties are nullable anyways'.
The ? in typescript isn't really about whether it is nullable - it is about whether it is required.

Optional Properties
Not all properties of an interface may be required. Some exist under certain conditions or may not be there at all. These optional properties are popular when creating patterns like “option bags” where you pass an object to a function that only has a couple of properties filled in.
https://www.typescriptlang.org/docs/handbook/interfaces.html

I am trying to use NSwagStudio to generate interfaces. For instance this is a simple interface that represents search criteria:

export interface SearchOrdersCriteria {
maxOrders?: number;
orderID: string;
username: string;
}

I want maxOrders to be optional when calling my client but it isn't being generated.

I agree that an interface in general should be to minimally implement its defined members, but I just want a strongly named / typed way to call my service with optional parameters. Typescript doesn't really treat interfaces like this so I think this change is a big mistake.

Why can't this be an option that I choose with for instance something like :

    [JsonProperty("maxOrders", Required = Required.AllowNull)]

from njsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.