harbor run -t openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-1995harbor download openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-1995PSEUDOCODE TASK
MODULE sanic_openapi.doc OBJECT doc
// 1) Field hierarchy for OpenAPI bodies/forms/paths CLASS Field CONSTRUCTOR(description=None, required=None, name=None, choices=None) METHOD serialize() -> DICT IF description IS NULL AND required IS NULL AND name IS NULL AND choices IS NULL RETURN {} result = {} IF description IS NOT NULL: result["description"] = description IF required IS NOT NULL: result["required"] = required IF name IS NOT NULL: result["name"] = name IF choices IS NOT NULL: IF TYPE(choices) IS LIST: result["enum"] = choices ELSE IF TYPE(choices) IS DICT AND "enum" IN choices: result["enum"] = choices["enum"] RETURN result
CLASS Integer INHERITS Field METHOD serialize() -> DICT RETURN {"type": "integer", "format": "int64"}
CLASS Float INHERITS Field METHOD serialize() -> DICT RETURN {"type": "number", "format": "double"}
CLASS String INHERITS Field METHOD serialize() -> DICT RETURN {"type": "string"}
CLASS Boolean INHERITS Field METHOD serialize() -> DICT RETURN {"type": "boolean"}
CLASS Date INHERITS Field METHOD serialize() -> DICT RETURN {"type": "string", "format": "date"}
CLASS DateTime INHERITS Field METHOD serialize() -> DICT RETURN {"type": "string", "format": "date-time"}
CLASS File INHERITS Field METHOD serialize() -> DICT RETURN {"type": "file"}
CLASS UUID INHERITS Field CONSTRUCTOR(name=None) METHOD serialize() -> DICT base = {"type": "string", "format": "uuid"} IF name IS NOT NULL: base["name"] = name RETURN base
CLASS JsonBody INHERITS Field METHOD serialize() -> DICT RETURN {"name": "body", "schema": {"properties": {}, "type": "object"}}
CLASS List CONSTRUCTOR(item_type=None) METHOD serialize() -> DICT IF item_type IS NULL: items = [] ELSE IF item_type IS Field subclass: instance = item_type() items = instance.serialize() ELSE IF item_type IS Field instance: items = item_type.serialize() ELSE: items = [] RETURN {"type": "array", "items": items}
// 2) Schema serialization helper FUNCTION serialize_schema(schema) -> DICT // Primitive primitives and Field abstractions IF schema IS Field class OR schema IS instance OF Field: RETURN {} IF schema IS int OR schema IS doc.Integer OR schema IS instance doc.Integer: RETURN {"type": "integer", "format": "int64"} IF schema IS float OR schema IS doc.Float OR schema IS instance doc.Float: RETURN {"type": "number", "format": "double"} IF schema IS str OR schema IS doc.String OR schema IS instance doc.String: RETURN {"type": "string"} IF schema IS bool OR schema IS doc.Boolean OR schema IS instance doc.Boolean: RETURN {"type": "boolean"} IF schema IS date OR schema IS doc.Date OR schema IS instance doc.Date: RETURN {"type": "string", "format": "date"} IF schema IS datetime OR schema IS doc.DateTime OR schema IS instance doc.DateTime: RETURN {"type": "string", "format": "date-time"} // Non-primitive user class IF TYPE(schema) IS type AND NOT IS_PRIMITIVE_TYPE(schema): RETURN {"$ref": "#/definitions/" + schema.name} // Dicts IF TYPE(schema) IS dict: RETURN {"type": "object", "properties": {}} // Example dicts: {"foo": "bar"} -> {"type": "object", "properties": {"foo": {}}} IF TYPE(schema) IS dict: props = {} FOR key IN schema.keys(): props[key] = {} RETURN {"type": "object", "properties": props} // Lists IF TYPE(schema) IS list: // Empty list -> {"type": "array", "items": []} IF LENGTH(schema) == 0: RETURN {"type": "array", "items": []} // Non-empty list -> {"type": "array", "items": {"description": schema}} RETURN {"type": "array", "items": {"description": schema}} // Fallback RETURN {}
// 3) OpenAPI decorator surface FUNCTION consumes(field, location="body" OR "formData" OR "path", required=False) // Attach OpenAPI parameter metadata to the Sanic route function without user code changes parameter = {} parameter["in"] = location parameter["required"] = required IF field IS INSTANCE OF JsonBody: // JsonBody shape appears as specified by tests parameter["name"] = "body" parameter["schema"] = {"properties": {}, "type": "object"} ELSE: parameter["name"] = field.name merged = field.serialize() // For body/formData/path, merge serialized field data into parameter FOR k, v IN merged.items(): parameter[k] = v ATTACH parameter TO route_function_metadata("consumes")
FUNCTION response(status_code, schema) ATTACH response_schema TO route_function_metadata("responses")
// 4) Automatic swagger endpoint generation WHEN Sanic app REGISTERS route THAT USES consumes CREATE/EXPOSE endpoint at "/swagger/swagger.json" BUILD swagger_spec AS OPENAPI-like DICT paths: FOR each registered_route path = sanitized Sanic path method = HTTP method (e.g., "GET") operation = {} operation["parameters"] = route_function_metadata("consumes") OR [] operation["responses"] = route_function_metadata("responses") OR {} // Path parameter transformation IF consumes parameters include location "path": TRANSFORM "/id:uuid" TO "/{id}" INCLUDE path parameter OBJECT: { "in": "path", "name": "id", "type": "string", "format": "uuid", "required": True } ADD operation TO swagger_spec.paths[path][method] definitions: INCLUDE definitions for non-primitive schemas as {"": {"type": "object", "properties": {}}} IF serialize_schema(...) yields a $ref RETURN swagger_spec AS JSON at GET "/swagger/swagger.json" with content-type application/json
// 5) API shape and compatibility MODULE importable AS sanic_openapi.doc OBJECT doc PROVIDES:
// 6) Acceptance criteria alignment // - Field serialization behavior matches test expectations // - Each field type serializes to exact dicts specified // - JsonBody serialization matches exact shape // - List field behavior matches test expectations (empty and typed items) // - serialize_schema() covers primitives, Field classes/instances, dicts, lists, and refs // - Swagger endpoint produces correct paths, parameters, $ref usage, and definitions // - Swagger JSON available at /swagger/swagger.json with application/json // - No external dependencies beyond Sanic; exact dictionary shapes for tests
// Notes // - Do not modify tests; API surface and data shapes must align precisely // - Output is self-contained and lightweight per tests