Loading…
Loading…
Describes how OpenAPI specification imports are persisted, deduplicated by content hash, and retrieved. Explains the data model and repository API.
The OpenApiImportRepository is a persistence component responsible for storing and managing records of imported OpenAPI specifications. It interfaces with the s2r_openapi_import database table to provide create and read operations for these records.
The primary problem this component solves is the efficient and idempotent storage of OpenAPI specifications. It achieves this through a content-based deduplication mechanism. When an API publisher uploads a specification, a unique hash of its content is calculated. The repository uses this hash to check if an identical specification has already been imported by the same tenant, preventing redundant storage and ensuring that re-uploads of the same file do not create new records.
This repository is an internal component used by the administrative service that handles the import of API specifications. It is not directly exposed to API consumers.
IMPORTANT
All repository operations are automatically scoped to the calling user's tenant. This ensures that one tenant cannot access or overwrite the import records of another, even if they upload an identical OpenAPI specification file.
The core logic of the repository is centered on the createOrReturnExisting method, which ensures that each unique specification file is stored only once per tenant.
When a request to import a new OpenAPI specification is processed, the system follows these steps:
content_hash) is computed from the raw bytes of the uploaded specification file.s2r_openapi_import table for an existing record with a matching content_hash within the current tenant's scope.info_title and info_version), the content hash, the raw content, and a normalized JSON representation of the spec (parsed_spec).id (UUID). Upon successful insertion, this new record is returned.DuplicateKeyException (because the other process just inserted a record with the same content_hash), the repository handles this by re-querying for the record by content_hash. It then returns the record that was just inserted by the concurrent process.This flow is illustrated in the following diagram:
flowchart TD
subgraph OpenAPI Import Process
A[Start: Upload OpenAPI Spec] --> B{Calculate content_hash};
B --> C{Query for existing record by content_hash};
C --> D{Record found?};
D -- Yes --> E[Return existing record];
D -- No --> F[Attempt to INSERT new record];
F --> G{INSERT successful?};
G -- Yes --> H[Return new record];
G -- No (DuplicateKeyException) --> I{Re-query for record by content_hash};
I --> J[Return re-queried record];
end
J --> Z[End];
E --> Z;
H --> Z;The OpenApiImportRepository class exposes the following public methods for interacting with OpenAPI import records.
createOrReturnExistingCreates a new import record or returns an existing one if a record with the same content_hash already exists for the tenant. This operation is idempotent.
Parameters
| Name | Type | Description |
|---|---|---|
sourceKind | String | The kind of source (e.g., 'file-upload', 'url-fetch'). |
sourceName | String | The name of the source, such as the original filename. |
sourceUri | String | The URI of the source, if applicable. |
content | byte[] | The raw byte content of the OpenAPI specification. |
contentType | String | The MIME type of the content (e.g., application/json). |
contentHash | String | A unique hash of the content. Used for deduplication. |
parsedSpecJson | String | A normalized, JSON-formatted string of the parsed specification. |
serversUrl | String | The server URL extracted from the specification's servers block. |
infoTitle | String | The title extracted from the specification's info block. |
infoVersion | String | The version extracted from the specification's info block. |
createdBy | String | The identifier of the user or principal who initiated the import. |
Returns
An OpenApiImportRecord object representing the newly created or pre-existing record.
findByIdRetrieves a single OpenApiImportRecord by its unique identifier (id). The search is scoped to the current tenant.
Parameters
| Name | Type | Description |
|---|---|---|
id | UUID | The unique identifier of the import record. |
Returns
An Optional<OpenApiImportRecord> which contains the record if found, or is empty if no record with that ID exists for the tenant.
findByHashRetrieves a single OpenApiImportRecord by its content_hash. The search is scoped to the current tenant.
Parameters
| Name | Type | Description |
|---|---|---|
contentHash | String | The content hash of the specification to search for. |
Returns
An Optional<OpenApiImportRecord> which contains the record if found, or is empty if no record with that hash exists for the tenant.
findParsedSpecJsonByIdFetches the normalized, parsed JSON string (parsed_spec) for a given import record ID. This provides access to the processed specification content stored in the database.
Parameters
| Name | Type | Description |
|---|---|---|
id | UUID | The unique identifier of the import record. |
Returns
An Optional<String> which contains the parsed JSON string if the record is found, or is empty otherwise.
The OpenApiImportRepository persists data in the s2r_openapi_import table. The OpenApiImportRecord object returned by query methods maps to a subset of these columns.
| Column | Type | Description |
|---|---|---|
id | UUID | The primary key for the record. |
source_kind | String | The origin of the import (e.g., 'file-upload'). |
source_name | String | The original name of the source, like a filename. |
source_uri | String | The URI from which the spec was imported, if applicable. |
content | bytea | The raw binary content of the specification file. Not returned in standard record queries. |
content_type | String | The MIME type of the uploaded specification. |
content_hash | String | A unique hash of the content field, used for deduplication within a tenant. |
parsed_spec | jsonb | The normalized JSON representation of the specification. Not returned in standard record queries; use findParsedSpecJsonById to retrieve it. |
servers_url | String | The primary server URL extracted from the spec. |
info_title | String | The title of the API, from the spec's info object. |
info_version | String | The version of the API, from the spec's info object. |
created_at | OffsetDateTime | The timestamp when the record was created. |
created_by | String | The identifier for the user who created the record. |
DuplicateKeyExceptionThe repository is designed to handle race conditions during concurrent uploads of identical files. If two processes attempt to create a record for the same content hash simultaneously, one will succeed, and the other will receive a DuplicateKeyException from the database.
This exception is caught and handled internally by the createOrReturnExisting method. The method will then re-fetch the record by its content_hash and return the data that the winning process successfully inserted. For the caller, this process is transparent and ensures the operation remains idempotent without returning an error.
The findById, findByHash, and findParsedSpecJsonById methods return an Optional. If the returned Optional is empty, it means no record matched the specified criteria for the current tenant. This is expected behavior and not an error condition.