Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

383
Visualizações
Customize generated model names - Swagger UI

I'm trying to adjust the "displayName" of the model being used in an automatically generated Swagger definition.

This will only affect the Swagger names, meaning the namespace in code would be left untouched, whilst when looking at the model from Swagger UI, you'd see a custom name.

Currently, the model name being returned from code is a namespace and looks something like this: b.c.d.e.f, I would like to add an attribute to the code and "mask" the name for the Swagger docs, so that when the documentation / Swagger definition gets generated it'll be displayed as CustomSwaggerName rather.

I have a few API's (C#) using tools that include Swashbuckle (preferred) and SwaggerGen, but right now, I'd just like to get it working in either, if at all possible.

I've tried using attributes that seem to look correct:

[ResponseType(typeof(Company)),DisplayName("NewCompany")]
[SwaggerResponse(200,"NewCompany",typeof(object))]

With no luck. I also browsed the SwashBuckle git repo, hoping to find something.

An image that should help further explain what i'm trying to achieve. enter image description here

I know this might seem like a strange use case but it's for a tool being written for our AWS API Gateway automation, which will use the Swagger definition for some comparisons.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

(Accounts for .net core:)
I would combine the display attribute of the class with a custom swagger schema:

[DisplayName("NewCompany")]
public class Company
{
}

and then in Startup:

services.AddSwaggerGen(c =>
{                     
    c.CustomSchemaIds(x => x.GetCustomAttributes<DisplayNameAttribute>().SingleOrDefault().DisplayName);
});

see also this answer

over 4 years ago · Santiago Trujillo Relatório

0

The answer is: use an IDocumentFilter:

private class ApplyDocumentFilter_ChangeCompany : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        if (swaggerDoc.definitions != null)
        {
            swaggerDoc.definitions.Add("Company123", swaggerDoc.definitions["Company"]);
        }
        if (swaggerDoc.paths != null)
        {
            swaggerDoc.paths["/api/Company"].get.responses["200"].schema.@ref = "#/definitions/Company123";
        }
    }
}

Here is the code: https://github.com/heldersepu/SwashbuckleTest/commit/671ce648a7cc52290b4ad29ca540b476e65240e6

And here is the final result: http://swashbuckletest.azurewebsites.net/swagger/ui/index#!/Company/Company_Get

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda