swagger-api / swagger-api/swagger-codegen

[NancyFx] Inheritance

Open
#7,079 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Currently the Nancy Generator does not support inheritance at all. I'll briefly discuss the current status of nancy w.r.t. inheritance based on the simple Pet/Cat model found further down.

Models:

The models inherit correctly from each other:

public class Pet:  IEquatable<Pet>{...}
public sealed class Cat: Pet,  IEquatable<Cat>{...}

However, the builder of the Cat currently doesn't allow setting the Pet properties (with immutable=true):

public sealed class CatBuilder {
  public CatBuilder HuntingSkil(string value) {...}
  public Cat Build() {...} 
  ... private members....
}
Deserialization:

The /pet endpoint deserializes subtypes (such as a Cat) currently wrong, i.e. it just serializes them as a Pet and ignores all additional properties of Cat:

Post["/pet"] = parameters =>
{
    var body = this.Bind<Pet>();
    Preconditions.IsNotNull(body, "Required parameter: 'body' is missing at 'PetPost'");
    
    return service.PetPost(Context, body);
};
Serialization:

Serialization works out of the box. That is a Cat is serialized as a Cat. The only problem is, that the discriminator is not automatically set (and because of the problem with model described above, it can in fact not be set at all).

Swagger-codegen version

2.3-Snapshot

Swagger declaration file content or url

Here is the swagger file that I have used above to describe the current status:

swagger: "2.0"
info:
  version: "1.0.0"
  title: Demo


paths:
  /pet:
    post:
      summary: "post a bar"
      parameters:
        - name: body
          in: body
          required: true
          schema: 
            $ref: '#/definitions/Pet'
      responses:
        201:
          description: successful.
          schema:
            $ref: '#/definitions/Pet'
  
          
definitions:
  Pet:
    discriminator: type
    type: object
    required:
      - type
    properties:
      type:
        type: string
      name:
        type: string
        
  Cat:
    allOf:
      - $ref: '#/definitions/Pet'
      - properties: 
          huntingSkil:
            type: string
Command line used for generation
-l nancyfx --additional-properties packageContext=v1,interfacePrefix=I
Related issues/PRs

I have not found a related issue/PR.

Suggest a fix/enhancement

I would be interested very much to fix this problem but I was hoping to get a bit of feedback before I start with it (@jimschubert ?, @mandrean ?).

Especially the deserialization issue is not so easy. At the moment I'm considering two possible solutions but maybe there are even better ideas...:

  1. Rely on json.net and the nancy integration. This seems to be the industry standard for c# and is also used by the Restsharp swagger client. However it would be an additional dependency. This was also suggested by the folks from the #nancyfx slack channel.
  2. Build a custom ModelBinder for every type with a discriminator. This will probably be a bit slower than the json.net implementation but it would avoid the introduction of an additional dependency.

In order to solve the problem with the model builder, I suggest to use the approach suggested here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the NancyFx generator entry point invoked by -l nancyfx and inspect how the Pet/Cat definitions with allOf and a discriminator are handled. Compare generated builders, model binding, and serialization for the sample /pet endpoint. Done means inheritance is supported for model construction and subtype deserialization while preserving the existing serialization behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.