Skip to content
← All posts
· Delta1 Labs ObfuscationNebula

Obfuscating an API without breaking its OpenAPI doc

Swashbuckle does not store your schema names anywhere — it reflects them off your DTO types at document-generation time. SchemaGeneratorOptions.SchemaIdSelector defaults to type.Name, so once an obfuscator renames LoginRequest to a, the OpenAPI components/schemas key becomes a and every $ref points at it. Worse, type.Name ignores the namespace, so two renamed types can both become a and Swashbuckle throws InvalidOperationException while serving /swagger/v1/swagger.json. This is the full mechanism: how schema IDs and property names are resolved, why renaming turns a green build into a 500 at the Swagger endpoint, and the exact nebula.config.json keep-list (or CustomSchemaIds escape hatch) that fixes it.

Here is a deployment that passes CI, starts cleanly, serves every endpoint correctly — and returns HTTP 500 the first time anything hits /swagger/v1/swagger.json. You added an obfuscation step to protect your API’s IP, the build went green, Postman still gets 200s from every route, and then your frontend’s code-generated client fails to regenerate with an error coming from deep inside Swashbuckle. Nothing is wrong with your API. Something is wrong with the description of your API, and the description is built by reflecting over the very type names your obfuscator just rewrote.

This is a subtle failure because the OpenAPI document is not part of the request path. It is generated on demand, by walking your DTO types with reflection, and it is the only part of your application that reads type and member names back out of metadata. So it is the only part that notices when those names change — sometimes by producing a meaningless document, and sometimes by refusing to produce one at all.

Where schema names actually come from

Swashbuckle builds the components/schemas section of your OpenAPI document from your CLR types. Two separate pieces of that section are derived from names:

  1. The schema ID — the key under components/schemas and the target of every $ref — comes from SchemaGeneratorOptions.SchemaIdSelector. Its default implementation is, in effect, type => type.Name:
// SwaggerGen defaults, paraphrased
public Func<Type, string> SchemaIdSelector { get; set; }
    = type => type.Name;   // "LoginRequest", not "Auth.LoginRequest"
  1. The property keys inside each schema come from the serializer’s data contract. Swashbuckle asks an ISerializerDataContractResolver for the JSON contract of the type; for System.Text.Json that resolver reads each property’s JsonPropertyInfo.Name, which is derived from the CLR property name (through your naming policy) unless a [JsonPropertyName] overrides it.

Neither name is stored anywhere ahead of time. Both are read out of metadata at the moment someone requests the document. That timing is the whole story: an obfuscator rewrites those names in the assembly, and Swashbuckle faithfully reports whatever the metadata now says.

Take an ordinary contract:

namespace Acme.Api.Contracts;

public sealed class LoginRequest
{
    public string Email { get; set; } = "";
    public string Password { get; set; } = "";
}

Before obfuscation the generated fragment is exactly what your client generator expects:

"LoginRequest": {
  "type": "object",
  "properties": {
    "email":    { "type": "string" },
    "password": { "type": "string" }
  }
}

After renaming, the type LoginRequest becomes a and its properties become a/b. The document is still valid OpenAPI — it is just unrecognisable:

"a": {
  "type": "object",
  "properties": {
    "a": { "type": "string" },
    "b": { "type": "string" }
  }
}

Every $ref: "#/components/schemas/LoginRequest" on every operation now reads #/components/schemas/a. The internal references stay consistent, so tools do not error — they simply generate a client class called a with properties a and b. The contract still functions on the wire (the serializer renamed the properties too, so runtime and document agree), but the meaning that made it a contract is gone.

The failure that is not silent

The schema-ID case has a sharper edge. type.Name deliberately ignores the namespace. Before obfuscation, Acme.Api.Auth.LoginRequest and Acme.Api.Admin.LoginRequest are two different types that happen to share a short name — and Swashbuckle already refuses that, which is why the common advice is to set CustomSchemaIds(t => t.FullName). Obfuscation creates the same collision from types that did not collide before: a renamer assigns short names per scope, so a public type in one namespace and a public type in another can both end up named a. Now SchemaIdSelector returns "a" for both, and SchemaRepository.RegisterType throws:

System.InvalidOperationException: Can't use schemaId "$a" for type
"$Acme.Api.a". The same schemaId is already used for type "$Acme.Api.b".

(The literal $ in that message is Swashbuckle’s own, from an interpolation quirk in SchemaRepository — a useful fingerprint when you see it in logs.) This exception is thrown while generating the document, inside the request for /swagger/v1/swagger.json, so the endpoint returns 500 while every real endpoint keeps serving. A health check that pings an API route stays green; only the OpenAPI document is down.

Document generation reads names from metadata at request timetypeof(LoginRequest)SchemaIdSelector → type.NameDataContract → property namesschemas: LoginRequest email, passwordAfter obfuscation renames the DTOstype.Name == "a"schema "a", props "a"/"b"valid, meaninglessTwo namespaces, one short name → collisionAuth.a + Admin.asame schemaId "a"throws → 500 /swaggertype.Name ignores the namespace, so types that did not collide before now do — but only theOpenAPI document breaks; routing, model binding and serialization are untouched.
Swashbuckle resolves schema IDs and property names from metadata when the document is requested, so it sees the obfuscated names — either meaningless schemas or, on a collision, a 500 at the swagger endpoint.

The fix: pin the contract, obfuscate the rest

Nebula’s rename pass is driven by nebula.config.json. Your request and response DTOs are a small, deliberately public surface — the one set of names that two independent consumers (the serializer on the wire and Swashbuckle in the document) both read back out of metadata. Carve them out and let everything else be renamed:

{
  "input": "bin/Release/net8.0/Acme.Api.dll",
  "rename": {
    "enabled": true,
    "keep": [
      {
        "namespace": "Acme.Api.Contracts",
        "kind": "type",
        "members": "public",
        "reason": "Swashbuckle derives schema IDs from type.Name and property keys from these public members; they are the API's OpenAPI contract"
      }
    ]
  },
  "strings": { "encrypt": true },
  "controlFlow": { "enabled": true }
}

The "members": "public" scope is the point: public type names and public property names on your contracts survive, so both the schema IDs and the property keys stay meaningful, while private backing fields — which neither the serializer nor Swashbuckle names — are still renamed. The controllers, services, validators and everything behind the contract are renamed, string-encrypted and control-flow-flattened like the rest of your code. You are pinning the data shapes the document is built from, not the logic.

The escape hatch: decouple the names from the types

If the DTO type names are themselves revealing and you want them renamed too, break Swashbuckle’s dependency on the CLR name. Give it a schema ID you control, and give each property an explicit wire name:

builder.Services.AddSwaggerGen(o =>
{
    // read a stable id from a literal you own, not from type.Name
    o.CustomSchemaIds(t =>
        t.GetCustomAttribute<SchemaNameAttribute>()?.Name ?? t.Name);
});

[SchemaName("LoginRequest")]
public sealed class LoginRequest
{
    [JsonPropertyName("email")]    public string Email { get; set; } = "";
    [JsonPropertyName("password")] public string Password { get; set; } = "";
}

"LoginRequest", "email" and "password" are now string literals. Nebula encrypts string literals but never renames them, so the type LoginRequest and its members are free to be renamed to a while the document — and the wire format — keep saying LoginRequest, email, password. This also removes the collision risk entirely: two renamed types can no longer produce the same schema ID, because the ID no longer comes from the name.

Caveats

The keep-list must cover every type that appears in the document, including nested DTOs, enums used as parameters, and the element type of any List<T> you return — a reference from a kept type to a renamed one still emits a schema named a. Generic response wrappers are a common trap: Swashbuckle composes their schema ID from the open type plus the argument names (ApiResponseOfLoginRequest), so if the wrapper is kept but the argument is not, you get ApiResponseOfa. Keep both. And remember the property-name case is consistency-preserving, not safe: because the serializer renamed the properties too, the document matches the wire, so there is no mismatch error — just a contract no human or generator can use. Only the schema-ID collision announces itself.

Takeaway

Swashbuckle is a reflection consumer that runs at request time, so it sees your obfuscated names and nothing else. Renaming leaves your API fully functional and leaves its OpenAPI document either meaningless or, when short names collide across namespaces, un-generatable. The dependency is narrow and name-based: pin the public members of the types that make up your contract in nebula.config.json, or move the names into string literals with CustomSchemaIds and [JsonPropertyName], then regenerate swagger.json and read it back with Glass to confirm the schema IDs survived. You ship an API whose internals are opaque to a reverse engineer and whose OpenAPI document still describes LoginRequest, not a. Protecting that API end to end is what Nebula.NET is for.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.