Skip to content

Support for Namespaces #90

@paule96

Description

@paule96

What would you like to be added:

It would be nice to organize components/schema keys in namesspaces if the name allows is.
So if you have a open api schema like this:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            maximum: 100
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        '201':
          description: Null response
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/PetStore.Pet"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    PetStore.Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      maxItems: 100
      items:
        $ref: "#/components/schemas/PetStore.Pet"
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

You can see the Open API name of the object Pet is PetStore.Pet.
Because this alone is not a valid C# type the generator currently removes the . so the C# model right now will get the following name: PetStorePet.

This feature request should change the behavior in this case to produce the following:

  • create the namespace PetStore
  • create in the namespace PetStore the class Pet

This is currently a feature that would make a cleaner support for OpenApi documents that were produced from languages that use like C# a . as a namespace divider.
The problem with this approach is, that namespaces are not supported by the open API specification, so this would be some magic this library does extra.

Why is this needed:

Currently in AutoSkd you can define models to exclude. (this switch is currently not working, I already started to implement a fix)
The problem is, if you exclude a model, a user of the AutoSdk needs to provide this model. For example over global usings. So the generated code can compile.
But if you have for example something like: System.Text.Json.Nodes.JsonObject as a model type to exclude you have no way to compile the generated result. Because the type used in the generated models is:

SystemTextJsonNodesJsonObject

The dots are removed

Implementation idea

Currently in the code all symbols like that:

['_', '-', '.', ' ', '\\', '/', '[', ']']

are removed from any csharp name that will be generated.
I would remove on any of these replacement methods the ..

Currently also in the object SchemaContext the id property get's multiple times overridden. I would suggest to use here the unique marker from the open api doc instead of something that has only to do with the csharp name. (better separation of concerns)

Next, I would like to implement ClassData on the SchemaContext more precisely so it can fulfill the following needs:

  • generate the logical namespace of the class it is representing (this means without the namespace that is maybe defined in the settings of the user)
  • generate the class name based on the following code: Sanetize(context.Reference.Id.Split('.').Last())

Then the TypeData can use this information to successfully build a type with the full namespace (settings.Namespace + '.' +classData.Namespace).
If the model is on the list of the exclusion, then the namespace in the ClassData should be used.

@HavenDV what is your opinion about it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions