yahehe / Nancy.Swagger

Nancy plugin for generated API documentation in Swagger format.
MIT License
133 stars 60 forks source link

Read only Model properties. #149

Closed vincentparrett closed 6 years ago

vincentparrett commented 6 years ago

Expected Behavior

Read Model properties show in response models.

Actual Behavior

Only Model properties with public getter & setters show up.

Specifications

We have separate model types for reponses and requests, and pretty much all the properties on response models are read only, or the setters are protected. This means they are not shown in the swagger model.

Obviously, changing the behavior to allow read only properties would probably break existing projects, so my suggestion would be to add a ModelKind property to the ModelAttribute class,

public enum ModelKind = (
  Default,
  ResponseOnly,
  RequestOnly
);

The ModelKind property would default to Default, which would keep the existing behavior. ResponseOnly would allow read only properties, RequestOnly would behave the same as Default (read & write properties).

At the moment I'm totally blocked by this issue, happy to submit a pull request.

jnallard commented 6 years ago

I'm fine with adding a new attribute parameter to Model. I don't like the enum though, as it seems specific to your use case. Maybe something more like:

public enum PropertyVisibility = (
  Public, //Just default to Public if the value is not supplied (that should prevent breaking)
  PublicReadonly,
  All //This is probably not needed
);

Feel free to rename them, but I would prefer them to be associated with the accesser modifiers (and not your use case)

vincentparrett commented 6 years ago

Now that I think about it, perhaps all that is needed is a bool property ShowReadOnlyProps - this would be simple to implement and much less confusing to use.

jnallard commented 6 years ago

That does sound better!

yahehe commented 6 years ago

Implemented by #151