Skip to content

Implement /pet/{petId}/uploadImage persistence and photo URL updates#10

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/implement-upload-image
Open

Implement /pet/{petId}/uploadImage persistence and photo URL updates#10
Copilot wants to merge 3 commits into
mainfrom
copilot/implement-upload-image

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 25, 2026

POST /pet/{petId}/uploadImage previously returned success without persisting anything or mutating pet data. This change wires the endpoint to store uploaded images under pet-images/{petId} and append the corresponding /photos/{petId}/{imageFileName} entry to the pet’s photoUrls.

  • Endpoint behavior

    • Implemented real upload handling in routes/pet/{petId}/uploadImage.ts.
    • Validates pet existence (404) and missing image content (400).
    • Persists image payload to pet-images/{petId}/{imageFileName}.
  • Filename resolution

    • Resolves imageFileName from request headers in priority order:
      • x-image-filename
      • x-file-name
      • content-disposition (filename* / filename)
      • fallback: image.bin
    • Uses path.basename(...) to avoid path traversal via header values.
  • Pet model mutation

    • Updates in-memory pet state by appending:
      • /photos/{petId}/{imageFileName}
    • Preserves existing photoUrls entries.
  • Coverage updates

    • Extended integration flow in test/dummy.test.js to assert:
      • successful upload response
      • photo URL appended on subsequent GET /pet/{petId}
      • file exists at expected disk path
    • Added test cleanup for generated pet-images artifacts.
const imageFileName = getFilenameFromHeaders($.x.headers);
const imageDirectory = path.resolve("pet-images", String($.path.petId));
await mkdir(imageDirectory, { recursive: true });
await writeFile(path.join(imageDirectory, imageFileName), imageContent);

$.context.petsById.set($.path.petId, {
  ...pet,
  photoUrls: [...pet.photoUrls, `/photos/${$.path.petId}/${imageFileName}`],
});

Copilot AI requested review from Copilot and removed request for Copilot May 25, 2026 17:19
Copilot AI linked an issue May 25, 2026 that may be closed by this pull request
Copilot AI requested review from Copilot and removed request for Copilot May 25, 2026 17:21
Copilot AI requested review from Copilot and removed request for Copilot May 25, 2026 17:26
Copilot AI changed the title [WIP] Add uploadImage endpoint for pet images Implement /pet/{petId}/uploadImage persistence and photo URL updates May 25, 2026
Copilot AI requested a review from pmcelhaney May 25, 2026 17:27
@pmcelhaney pmcelhaney marked this pull request as ready for review May 25, 2026 17:41
Copilot AI review requested due to automatic review settings May 25, 2026 17:41
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes the Swagger Petstore simulator’s POST /pet/{petId}/uploadImage endpoint actually persist the uploaded image to disk and update the in-memory pet record’s photoUrls accordingly.

Changes:

  • Implemented upload handling in routes/pet/{petId}/uploadImage.ts (validate pet existence/body, save file under pet-images/{petId}, append /photos/{petId}/{filename}).
  • Extended the integration test to upload an image and assert both the pet mutation and file persistence, including cleanup of generated artifacts.
  • Updated package-lock.json to align with the package name and dependency classification.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 3 comments.

File Description
swagger-pet-store/routes/pet/{petId}/uploadImage.ts Adds image filename resolution, payload extraction, disk persistence, and photoUrls mutation.
swagger-pet-store/test/dummy.test.js Adds integration coverage for upload + persistence and deletes pet-images after the test run.
swagger-pet-store/package-lock.json Syncs lockfile metadata/dependency flags with the package configuration.
Files not reviewed (1)
  • swagger-pet-store/package-lock.json: Language not supported

Comment on lines +5 to +28
const getFilenameFromHeaders = (headers: Record<string, string>): string => {
const explicitFileName =
headers["x-image-filename"] ?? headers["x-file-name"];
if (explicitFileName) {
return path.basename(explicitFileName);
}

const contentDisposition = headers["content-disposition"];
if (!contentDisposition) {
return "image.bin";
}

const utf8Match = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i);
if (utf8Match) {
return path.basename(decodeURIComponent(utf8Match[1]));
}

const standardMatch = contentDisposition.match(/filename="?([^";]+)"?/i);
if (standardMatch) {
return path.basename(standardMatch[1]);
}

return "image.bin";
};
Comment on lines +5 to +28
const getFilenameFromHeaders = (headers: Record<string, string>): string => {
const explicitFileName =
headers["x-image-filename"] ?? headers["x-file-name"];
if (explicitFileName) {
return path.basename(explicitFileName);
}

const contentDisposition = headers["content-disposition"];
if (!contentDisposition) {
return "image.bin";
}

const utf8Match = contentDisposition.match(/filename\*=UTF-8''([^;]+)/i);
if (utf8Match) {
return path.basename(decodeURIComponent(utf8Match[1]));
}

const standardMatch = contentDisposition.match(/filename="?([^";]+)"?/i);
if (standardMatch) {
return path.basename(standardMatch[1]);
}

return "image.bin";
};
return Uint8Array.from(body.data);
}

return new Uint8Array();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement uploadImage

3 participants